Design Pattern is a re-usable, high quality solution to a given requirement, task or recurring problem. Further, it does not comprise of a complete solution that may be instantly converted to a code component, rather it provides a framework for how to solve a problem. Design Patterns are in a continuous phase of evolution, which means that they keep on getting better & better as they are tested against time, reliability and subjected to continuous improvements.
Design Patterns are categorized into 3 types:
- Creational Patterns
- Structural Patterns
- Behavioral Patterns
Creational Patterns: These patterns specify how objects are created and utilized in an application. They tackle the aspects of when and how objects are created, keeping in mind whats the best way these objects should be created.
Listed below are some of the commonly known Creational Design Patterns:
- Abstract Factory Pattern
- Factory Pattern
- Builder Pattern
- Lazy Pattern
- Prototype Pattern
- Singleton Pattern
In an abstract factory design, a framework is provided for creating sub-components that inherit from a common component. In .NET, this is achieved by creating classes that implement a common interface or a set of interfaces, where the interface comprises of the generic method declarations that are passed on to the sub-components.
Note that the term “Factory” refers to the location in the code where the code is created.
A Factory Pattern is again an Object creation pattern. Here objects are created without knowing the class of the object. Actually this means that the object is created by a method of the class, and not by the class’s constructor. So basically the Factory Pattern is used wherever sub classes are given the priviledge of instantiating a method that can create an object.
In a builder design pattern, an object creation process is separated from the object design construct. This is useful because the same method that deals with construction of the object, can be used to construct different design constructs.
The approach of the Lazy Design Pattern is not to create objects until a specific requirement matches, and when it matches, object creation is triggered. A simple example of this pattern is a Job Portal application. Say you register yourself in that site thus filling up the registration table, only when the registration table is filled, the other objects are created and invoked, that prompt you to fill in other details too, which will be saved in other tables.
A prototype design pattern relies on creation of clones rather than objects. Here, we avoid using the keyword ‘new’ to prevent overheads.
The Singleton design pattern is based on the concept of restricting the instantiation of a class to one object. Say one object needs to perform the role of a coordinator between various instances of the application that depend on a common object, we may design an application using a Singleton. A singleton class may be used to instantiate an object of it, only if that object does not already exist. In case the object exists, a reference to the existing object is given. A singleton object has one global point of access to it.
Structural Patterns: A structural design pattern establishes a relationship between entities. Thus making it easier for different components of an application to interact with each other. Following are some of the commonly known structural patterns:
Adapter Pattern – Interfaces of classes vary depending on the requirement.
Bridge Pattern – Class level abstraction is separated from its implementation.
Composite Pattern – Individual objects & a group of objects are treated similarly in this approach.
Decorator Pattern – Functionality is assigned to an object.
Facade Pattern – A common interface is created for a group of interfaces sharing a similarity.
Flyweight Pattern – The concept of sharing a group of small sized objects.
Proxy Pattern – When an object is complex and needs to be shared, its copies are made. These copies are called the proxy objects.
Behavioral Patterns: Behavioral design patterns focus on improving the communication between different objects. Following are different types of behavioral patterns:
Chain Or Responsibilities Pattern – In this pattern, objects communicate with each other depending on logical decisions made by a class.
Command Pattern – In this pattern, objects encapsulate methods and the parameters passed to them.
Observer Pattern – Objects are created depending on an events results, for which there are event handlers created