Design patterns are known as best practices that the programmer can use to solve common problems when designing an application or system.
Singelton Design Pattern
Factory Design Pattern
Builder Design Pattern
Strategy Design Pattern
Iterator Design Pattern
Observer Design Pattern
Prototype Design Pattern
Command Design Pattern
Decorator Pattern
Proxy Pattern
Adapter Pattern
1. Design patterns are known as best practices that the programmer can use to solve common problems when designing an
application or system.
2. Classification of Design Patterns
Creational
Creational patterns provide various object creation mechanisms, which increase flexibility and reuse of
existing code.
Structural
Structural patterns explain how to assemble objects and classes into larger structures while keeping these
structures flexible and efficient.
Behavioral
Behavioral patterns take care of effective communication and the assignment of responsibilities between
objects.
3. Singleton Design Pattern
Creational(1/4)
Is a creational design pattern that lets you ensure
that a class has only one instance, while providing
a global access point to this instance.
A Singleton encapsulates a unique resource and
makes it readily available throughout the
application
4. Singleton Design Pattern
Creational(1/4)When we use it ?
- When a class in your program should have just a single
instance available to all clients; for example, a single
database object shared by different parts of the program.
-Similarly, there can be a single configuration manager or
error manager in an application that handles all problems
instead of creating multiple managers.
-Centralise logging staff
5. Singleton Design Pattern
Creational(1/4)Bad things?
-Create a dependance for other classes who create the
instance
-Classes that depend on singletons are relatively harder
to unit test in isolation.Directly impacting testability and
maintainability
-The Singleton design pattern promotes tight coupling
between the classes in your application
The pattern requires special treatment in a
multithreaded environment so that multiple threads
won’t create a singleton object several times.
7. Factory Design Pattern
Creational(2/4)
Factory is a creational design pattern that provides an
interface for creating objects in a superclass, but allows
subclasses to alter the type of objects that will be created.
-Allows the consumer to create new objects without having
to know the details of how they're created, or what their
dependencies are -. they only have to give the information
they actually want.
8. Factory Design Pattern
Creational(2/4)When we use it ?
- You don’t know beforehand the exact types and
dependencies of the objects your code should work with.
-Construction is very complex and you need to reuse it.
- You want to save system resources by reusing existing
objects instead of rebuilding them each time.
9. Factory Design Pattern
Creational(2/4)When NOT use it ?
- We don’t have any complex creation logic.
if the factory doesn’t make any decision, the creation logic
is very simple or is used only in a single place, the factory
will be a needless abstraction.
11. Builder Design Pattern Creational(3/4)
Builder pattern builds a complex object using simple objects
and using a step by step approach.The pattern allows you to
produce different types and representations of an object
using the same construction code.
This builder is independent of other objects.
12. Builder Design Pattern Creational(3/4)
When we use it ?
-When you want your code to be able to create different
representations of some product.
-When you want isolate complex construction code from the
business logic of the product.
15. Prototype Design Pattern
Creational(4/4)When we use it ?
-when creation of object directly is costly (heavy process).
For example, an object is to be created after a costly
database operation. We can cache the object, returns its
clone on next request and update the database as and when
needed thus reducing database calls.
Your code shouldn’t depend on the concrete classes of
objects that you need to copy.
17. Decorator Design Pattern Structural(1/3)
Decorator is a structural design pattern that lets you attach
new behaviors to objects by placing these objects inside
special wrapper objects that contain the behaviors.
The decorator pattern allows a user to add new functionality
to an existing object without altering its structure.
18. Decorator Design Pattern Structural(1/3)
When we use it ?
-When you need to be able to assign extra behaviors to
objects at runtime without breaking the code that uses these
objects.
-When it’s difficult or not possible to extend an object’s
behavior using inheritance.
20. Proxy Design Pattern Structural(2/3)
Proxy is a structural design pattern that lets you provide a
substitute or placeholder for another object.
A proxy controls access to the original object, allowing
you to perform something either before or after the
request gets through to the original object.
The proxy could interface to anything: a network
connection, a large object in memory, a file, or some other
resource that is expensive or impossible to duplicate
21. Proxy Design Pattern Structural(2/3)
When we use it (1/2) ?
you want only specific clients to be able to use the
service object; for instance, when your objects are central
parts of an operating system and clients are various
launched applications (including malicious ones).
The proxy can pass the request to the service object only
if the client’s credentials match some criteria.(control
proxy)
Logging requests (logging proxy). This is when you want
to keep a history of requests to the service object.
The proxy can log each request before passing it to the
service.
22. Proxy Design Pattern Structural(2/3)
When we use it (2/2) ?
Caching request results (caching proxy). This is when you
need to cache results of client requests and manage the
life cycle of this cache, especially if results are quite large.
The proxy can implement caching for recurring requests
that always generate the same results
24. Adapter Design Pattern Structural(3/3)
Adapter is a structural design pattern that allows objects with
incompatible interfaces to collaborate.
This pattern involves a single class which is responsible to join
functionalities of independent or incompatible interfaces.
The Adapter pattern lets you create a middle-layer class that
serves as a translator between your code and a legacy class, a
3rd-party class or any other class with a weird interface.
25. Adapter Design Pattern Structural(3/3)
When we use it ?
-Use the Adapter class when you want to use some existing
class, but its interface isn’t compatible with the rest of your
code.
-Use the pattern when you want to reuse several existing
subclasses that lack some functionality that can’t be added to
the superclass.
-
27. Iterator Design Pattern
Behavioral(1/4)
This pattern is used to get a way to access the elements of a
collection object in a sequential manner without any need to know
its underlying representation. (list, stack, tree, complex data
structures.).
As name implies, iterator helps in traversing the collection of objects
in a defined manner which is useful the client applications.
28. Iterator Design Pattern
Behavioral(1/4)When we use it ?
-when your collection has a complex data structure under the hood,
but you want to hide its complexity from clients (either for
convenience or security reasons).
-when you want your code to be able to traverse different data
structures or when types of these structures are unknown
beforehand.
30. Observer Design Pattern Behavioral(2/4)
Observer is a behavioral design pattern that lets you define a
subscription mechanism to notify multiple objects about any events
that happen to the object they’re observing.
31. Observer Design Pattern Behavioral(2/4)
When we use it ?
-When some objects in your app must observe others, but only for a
limited time or in specific cases.
-When changes to the state of one object may require changing
other objects, and the actual set of objects is unknown beforehand or
changes dynamically.
33. Command Design Pattern Behavioral(3/4)
Command is a behavioral design pattern that turns a
request into a stand-alone object that contains all
information about the request. This transformation lets
you parameterize methods with different requests, delay
or queue a request’s execution, and support undoable
operations.
34. Command Design Pattern Behavioral(3/4)
-When you want to queue operations, schedule their
execution, or execute them remotely.
-when you want to implement reversible operations.
(undo/redo, the Command pattern is perhaps the most
popular of all.)
-Use the Command pattern when you want to parametrize
objects with operations.
( you can pass commands as method arguments, store
them inside other objects, switch linked commands at
runtime, etc.)
36. Strategy Design Pattern Behavioral (4/4)
Strategy is a behavioral design pattern that lets you
define a family of algorithms, put each of them into a
separate class, and make their objects
interchangeable.
37. Strategy Design Pattern Behavioral (4/4)
When we use it ?
-you want to use different variants of an algorithm
within an object and be able to switch from one
algorithm to another during runtime.
-you have a lot of similar classes that only differ in
the way they execute some behavior.
A toolkit of typical and tested solutions to common problems in software design.
- Even if you never encounter these problems, knowing patterns is still useful because it teaches you how to solve all sorts of problems using principles of object-oriented design.
- Design patterns define a common language that you and your teammates can use to communicate more efficiently. You can say, “Oh, just use a Singleton for that,” and everyone will understand the idea behind your suggestion. No need to explain what a singleton is if you know the pattern and its name.
are all about class instantiation or object creation. [Singelton / Factory /builder / Prototype ]
are about organizing different classes and objects to form larger structures and provide new functionality. [ Decorator / Proxy /Adapter ]
about identifying common communication patterns between objects [iterator / Observer /Command /Strategy]