Design patterns are solutions to software design problems you find again and again in real-world application development. Patterns are about reusable designs and interactions of objects. The 23 Gang of Four (GOF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups:
Design patterns have two major benefits.
- They provide you with a way to solve issues related to software development using a proven solution.
- Design patterns make communication between designers more efficient.
Why should I learn patterns?
The truth is that you might manage to work as a programmer for many years without knowing about a single pattern. A lot of people do just that. Even in that case, though, you might be implementing some patterns without even knowing it. So why would you spend time learning them?
- Design patterns are a toolkit of tried 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.
Creational Design Pattern: Provide object creation mechanisms that increase flexibility and reuse of existing code.
The Creational design patterns are categorized into two types. They are as follows:
1. Object-creational patterns: The Object-creational patterns deal with object creation. Here, it defers part of its object creation to another object.
2. Class-creational patterns: The Class-creational patterns deal with class-instantiation. Here, it defers its object creation to sub-classes.
When to use the Creational Design Pattern?
In real-time applications, the project is created with a lot of classes. A lot of classes mean we are going to deal with a lot of objects. If these objects creations are scattered on the client code, then it leads to a lot of complicated logic at the client code. The Creational Design Pattern helps us to centralize the object creation logic.
The creational Design Pattern is divided into the five categories:
Abstract Factory: Creates an instance of several families of classes.
Builder Separates: Object construction from its representation.
Factory Method: Creates an instance of several derived classes.
Prototype: A fully initialized instance to be copied or cloned.
Singleton: A class of which only a single instance can exist.
Structural Design Pattern: Explain how to assemble objects and classes into larger structures, while keeping the structures flexible and efficient.
When to use Structural Design Patterns in C#?
In real-time applications, sometimes we need to change the structure of a class or the relationship among the classes but we don’t want this change to be affected by the project. For example, if we have two classes let say user and product. And the product class is used inside the user class making one to many relationships between the User and Product. Tomorrow, the structure or the relationships between these two classes change. The customer now wants to keep away the product class from the User class, as they want to use the User and Product class independently. This is actually a structural change and we don’t want this structural change to affect our project.
The Structural Design Pattern is divided into the seven categories:
Adapter: Match interfaces of different classes
Bridge: Separates an object’s interface from its implementation
Composite: A tree structure of simple and composite objects
Decorator: Add responsibilities to objects dynamically
Facade: A single class that represents an entire subsystem
Flyweight: A fine-grained instance used for efficient sharing
Proxy: An object representing another object
Behavioral patterns: Take care of effective communication and the assignment of responsibilities between objects.
When to use Behavioral Design Patterns in C#?
In real-time applications, sometimes we want to change the behavior of a class and again we don’t want it to affect other classes of the project. For example, we have an Invoice class which currently applying taxes as 18%. Tomorrow if we want to add another extra tax. That means we are changing the behavior of a class. To solve such type of Behavioral issues Behavioral Design Pattern comes into the picture.
The Behavioral Design Pattern is divided into the several categories:
Chain of Resp.: A way of passing a request between a chain of objects
Command: Encapsulate a command request as an object
Interpreter: A way to include language elements in a program
Iterator: Sequentially access the elements of a collection
Mediator: Defines simplified communication between classes
Memento: Capture and restore an object's internal state
Observer: A way of notifying change to a number of classes
State: Alter an object's behavior when its state changes
Strategy: Encapsulates an algorithm inside a class
Template Method: Defer the exact steps of an algorithm to a subclass
Visitor: Defines a new operation to a class without change
If you want know more about Design Pattern with Example you can use this URL