There are different classifications/categoris of design pattern when we group together
The classification of design patterns keep growing, this is only exhaustive list.
Different types of design patterns:
1. Abstract factory: Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
2. Builder: Separate the construction of a complex object from its representation allowing the same construction process to create various representations.
3. Factory method: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
4. Lazy initialization: Tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.
5. Multiton: Ensure a class has only named instances, and provide global point of access to them.
6. Object pool: Avoid expensive acquistion and release of resources by recycling objects that are no longer in use. Can be considered a generalisation of connection pool and thread pool patterns.
7. Prototype: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
8. Resource acquisition is initialization: Ensure that resources are properly released by tying them to the lifespan of suitable objects.
9. Singleton: Ensure a class has only one instance, and provide a global point of access to it.
1. Adapter or Wrapper or Translator: Convert the interface of a class into another interface clients expect. An adapter lets classes work together that could not otherwise because of incompatible interfaces. The enterprise integration pattern equivalent is the translator.
2. Bride: Decouple an abstraction from its implementation allowing the two to vary independently.
3. Composite: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.
4. Decorator: Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality.
5. Facade: Provide a unified interface to a set of interfaces in a subsystem. Facade defines a high-level interface that makes the subsystem easier to use.
6. Flyweight: Use sharing to support large numbers of similar objects efficiently.
7. Front Controller: The pattern relates to the design of Web applications. It provides a centralized entry point for handling requests.
8. Module: Group several related elements, such as classes, singletons, methods, globally used, into a single conceptual entity.
9. Proxy: Provide a surrogate or placeholder for another object to control access to it.
1. Blackboard: Generalized observer, which allows multiple readers and writers. Communicates information system-wide.
2. Chain of responsibility: Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
3. Command: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log request, and support undoable operations.
4. Interpreter: Given a language, define a representation for its grammar alond with an interpreter that uses the representation to interpret sentences in the language.
5. Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
6. Mediator: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicityly, and it lets you vary their interaction independently.
7. Memento: Without violating encapsulation, capture and externalize an objects’s internal state allowing the object to be restored to this state later.
8. Null object: Avoid null references by providing a default object.
9. Observer or Publish/subscribe: Define a one-tomany dependency between objects where a state change in one object results in all its dependents being notified and updated automatically.
10.Servant: Define common functionality for a group of classes.
11.Specification: Recombinable business logic in a Boolean fashion.
12.State: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.
13.Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
14.Template method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
15.Visitor: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
1. Active Object: Decouples method execution from method invocation that reside in their own thread of control. The goal is to introduce concurrency, by using asynchronous method invocation and a scheduler for handling requests.
2. Balking: Only execute an action on an object when the object is in a particular state.
3. Binding Properties: Combining multiple observers to force properties in different objects to be synchronized or coordinated in some way.
4. Double-checked locking: Reduce the overhead of acquiring a lock by first testing the locking criterion (the ‘lock hint’) in an unsafe manner; only if that succeeds does the actual lock proceed. Can be unsafe when implemented in some language/hardware combinations. It can therefore sometimes be considered an anti-pattern.
5. Event-based asynchronous: Addresses problems with the asynchronous pattern that occur in multithreaded programs.
6. Guarded Suspension: Manages operations that require both a lock to be acquired and a precondition to be satisfied before the operation can be executed.
7. Lock: One thread puts a “lock” on a resource, preventing other threads from accessing or modifying it.
8. Messaging design pattern (MDP): Allows the interchange of information (i.e. messages) between components and applications.
9. Monitor object: An object whose methods are subject to mutual exclusion, thus preventing multiple objects from erroneously trying to use it at the same time.
10.Reactor: A reactor object provides an asynchronous interface to resources that must be handled synchronously.
11.Read-write lock: Allows concurrent read access to an object, but requires exclusive access for write operations.
12.Scheduler: Explicitly control when threads may execute single-threaded code.
13.Thread pool: A number of threads are created to perform a number of tasks, which are usually organized in a queue. Typically, there are many more tasks than threads. Can be considered a special case of the object pool pattern.
14:Thread-specific storage: Static or global memory local to a thread.
-> Factory Method
-> Lazy instatiation
-> Utility Pattern
Creational pattern talks about how we instatiate an object. Different patterns for different ways of instantiation.
Lets check some of the commonly used patterns:
Chain of Responsibility
Decorator Design Pattern
Lazy Load Pattern
Model View Presenter (MVP) Pattern
Model View ViewModel (MVVM) Pattern
Null Object Pattern
The Prototype Pattern
Service Locator Pattern
Unit of Work
Lets get the brief introduction of the above mentioned patterns
Adapter Pattern: Adapters are commonly known as wrappers. It’s basically like to when you want to use some methods in a library but the objects or parameters are not the same as in your application then you’ll create a wrapper/adapter which takes the parent/host library methods and convert them to the way that your application can utilize it. It’s called adapter pattern.
Like AC – to – DC adapter we use in our day to day life or voltage adapter when we travel to different countries for your electronic appliances.
Bridge Pattern: “Decouple an abstraction from its implementation so the two can vary independently” as per Gangs of Four. In simple terms, when you are using an object in combination with some other objects rather than providing all the possible combinations you can create an abstraction for each object and then let the user decide on the combination so that at any point of time in the future if you want to add any new items to any of the objects it should not affect the combination. Since the combination is still possible. (Ex: different Burgers and fries & drink or different Burgers and Vegetables & milk.. if you create an abstraction for burgers and fries & drink or vegetables & milk then you can create any number of combinations even if you add another kind of burger or another combination of drink like vegetables & drink).
Builder Pattern: “Separates the construction of a complex object from its representation so that the same construction process can create different representations.” as per Gangs of Four. In simple software terms, separate the data from logic and reuse that logic. Instead of giving the entire steps of a process which is to be repeated, create a logic for that and reuse it. (Ex: going to subway and giving the same instruction again and again whenever you go there create a pattern and just mentioning the pattern that you like to have each time you go there.) simplify the process.
Chain of Responsibility: “Ordered list of message handlers which knows how to do things”. The process goes in this way: Sender gets a message which need to be deciphered and send the response, sender is aware of receiver 1 in the chain as there are multiple receivers in the chain and Sender sends the message to receiver 1 to decipher, as Receiver 1 is not able to decipher that it will send to the next level of receiver Receiver 2 and receiver 2 is capable of deciphering it and sends the response to the sender. Sender is not aware about the receiver 2 and the higher levels of receiver 2 are not aware of the message. It’s like every level has its responsibility and if it’s not capable of resolving the problem it will send to the next higher level and next higher level. Whichever level is able to resolve the problem, will send the response directly to the initial layer raising the problem. Best like Linked list.
The Command Pattern: Also known as Action/Transaction Pattern. Represent an action/logic/operation as an object. Decouple clients that execute the command from the details and dependencies of the command logic. It enables delayed execution.
Factory Patterns: Consider factories when we are unsure of the implementation of an interface I want to return for a given class or creation should be separated from representation of an object or lots of if/else statements when deciding which concrete class to create or switch statements when deciding which concrete class to create:
3 different types of factories available
1. Simple Factory Pattern: Encapsulate object creation. Allows for late-bound decisions regarding instationation (configuration based, other persistent storage, input or other dynamic data). Caller class knows what concrete factory it needs.