Once in a while, every developer goes through the term design pattern, analyzes these patterns and eventually uses them in various code. So the question first arises in mind as to what’s the design pattern, what did they do and how did they do it. We start with answering the first question here.
Design Pattern is a general reusable solution to a commonly occurring problem within a given context.
What exactly does this mean ??? Let's try to understand.
What did we do when we get a problem to solve which is new to us? Google it and check if somebody already solved our problem. Exactly like this case, each design pattern has an already available solution to solve some particular problems that we encounter while designing logic or writing code for our applications.
Now let’s explore various types of Design Patterns. Design patterns are mainly categorized into three types:
- Creational Patterns: Creational patterns are used to construct objects such that they can be decoupled from their implementing system.
Examples of creational patterns are:
Structural Patterns: Structural patterns are used to form large object structures between many disparate objects.
- Abstract Factory: Create instances of classes belonging to different families
- Builder: Separate representation and object construction
- Factory: Create instances of derived classes
- Prototype: Clone or copy initialized instances
- Singleton: Class with only one single possible instance
Examples of structural patterns are:
Behavioral Patterns: Behavioral Patterns are used to manage algorithms, relationships, and responsibilities between objects.
- Adapter: Match interfaces of classes with different interfaces
- Bridge: Separate implementation and object interfaces
- Composite: Simple and composite objects tree
- Decorator: Dynamically add responsibilities to objects
- Facade: Class that represents subclasses and subsystems
- Flyweight: Minimize memory usage by sharing as much data as possible with similar objects
- Proxy: Object that represents another object
Examples of behavioral patterns are:
- Chain of Responsibility: Pass requests between command and processing objects within a chain of object
- Command: Encapsulate a method call as an object containing all necessary information
- Interpreter: Include language elements and evaluate sentences in a given language
- Iterator: Give sequential access to elements in a collection
- Mediator: Encapsulates and simplifies communication between objects
- Memento: Undo modifications and restore an object to its initial state
- Observer: Notify dependent objects of state changes
- State: Change object behavior depending on its state
- Strategy: Encapsulate algorithms within a class and make them interchangeable
- Template: Define an algorithm skeleton and delegate algorithm steps to subclasses so that they may be overridden
- Visitor: Add new operations to classes without modifying them
In design pattern, we often listen to the term Gang of Four, GOF, or GO4. Gang of four is nothing to do with technical sense, it’s name of four authors(Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides) of book Design Patterns: Elements of Reusable Object-Oriented Software.
In the following posts, I will pick each one of the original 23 GOF design patterns, discuss the pros, cons and need, UML diagrams for patterns and simple code illustration in C#.ject