Design Patterns in C#
Design Patterns are nothing but, you can say, documented and tested solutions for recurring problems in a given context.
In simple words Design Patterns are reusable solutions to the problems that, as a developer, we encounter in our day-to-day programming.
Design patterns provide general solutions or a flexible way to solve common design problems.
Each design pattern has pros and cons
History and Evolution of Design Patterns
The four authors of the book, famously known as the Gang of Four, brought the concepts of design patterns
Types of Design Patterns
Design Pattern are categorized into three main categories based on the three problem areas (Object Creation and Initialization, Structural Changes of Classes, and Interfaces, and the Relationship Between Classes, Communication Between Objects) of software architecture.
1. Creational Design Pattern (Object Creation and Initialization)
2. Structural Design Pattern (Structural Changes of Classes, and Interfaces, and the Relationship Between Classes)
3. Behavioral Design Pattern (Communication Between Objects)
There are 23 design patterns in total.
Creational Design Patterns:
The Creational Design Pattern deals with Object Creation and Initialization.
The Creational Design Pattern gives the programmer more flexibility in deciding which objects need to be created for a given case.
The Creational Design Pattern helps us to centralize the object creation and initialization logic, and depending upon the condition, it will create and initialize the appropriate object and returns that object to the client, and then the client can consume the object by calling the necessary methods and properties. The client does not know how the object is created and initialized.
For instance, if we have a large project, it implies we have a lot of classes, and dealing with a lot of objects. Therefore, depending on certain criteria, we must build several objects (such as new User(), new Product(), new Invoice(), etc.).
Creational Design Patterns Categories as:- Singleton, Factory, Builder, Prototype, Fluent Interface, Factory Method, and Abstract Factory.
Structural Design Patterns:
The Structural Design Pattern is used to Manage the Structure of Classes and Interfaces and the Relationship Between the Classes and Interfaces.
For example, if we have a User and Product class and the Product class is used inside the User class making One-to-Many relationships.
As the project grows tomorrow, we need to seperate the product class from the User class as we want to use the Product and Customer classes independently.
This is a structural change, but we don’t want this structural change to affect our project. This is where the Structural Design Pattern helps us.
Structural Design Patterns Categories as:- Adapter, Facade, Decorator, Composite, Proxy, Flyweight, and Bridge Design Patterns.
Behavioral Design Patterns:
Behavioral Design Patterns deal with the Communication Between Classes and Objects.
This implies that if you change a class's behaviour in future, you need the change to have an impact on other related project's classes as well. For instance, the tax rate that is presently used on your Invoice class is 18%. If you need to add yet another tax, do so tomorrow, then what?. By doing so, you are modifying a class's behaviour. Behavioural Design patterns can help tackle these behavioural problems.
Behavioral Design Patterns Categories as:- Chain of Responsibility, Command, Observer, Iterator, State, Template Method, Visitor, Strategy, Mediator, Memento, and Interpreter Design Pattern.