You are on page 1of 2

Design Patterns in C#

Design Patterns are reusable solutions to the problems that, as a developer, we encounter in
our day-to-day programming / occur in software design. Design Patterns are used to solve
the problems of Object Generation and Integration. They represent best practices and have
evolved over time through trial and error by experienced software developers.

Types of Design Patterns


Design Pattern 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 and communication Between Objects) of software
architecture. They are as follows.

Creational Design Patterns:


The Creational Design Pattern deals with Object Creation and Initialization. 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
return that object to the client. Then, the client can consume the object by calling the
necessary methods and properties.
For example, if we have a huge project, a huge project means we have a lot of classes, and
a lot of classes means we are dealing with many objects. So we need to create different
objects (like new Customer(), new Product(), new Invoice(), etc.) based on some
conditions. If the object creations and initialization logic are not centralized, it leads to a
complicated client code.
Examples : 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 Customer and Product class and the Product class is used inside the Customer
class, making One-to-Many relationships. If tomorrow, we want to use the Product and
Customer classes independently. This is a structural change, and we don’t want this
structural change to affect our project. This is where the Structural Design Pattern helps us.
These patterns concern how classes and objects can be composed to form larger structures.
They help ensure that when one part of a system changes, the entire structure of the system
doesn’t need to change.
Examples: Adapter, Facade, Decorator, Composite, Proxy, Flyweight,
and Bridge Design Patterns.

Behavioral Design Patterns:


Behavioral Design Patterns deal with the Communication Between Classes and
Objects. That means if you want to change the behavior of a class again, you want it to
affect other classes of the project as well. For example, you have an Invoice class that
currently applies taxes as 18%. Tomorrow, if you have to add another extra tax. That means
you are changing the behavior of a class. To solve such Behavioral issues, Behavioral
Design patterns come into the picture.
Examples: Chain of Responsibility, Command, Observer, Iterator, State, Template
Method, Visitor, Strategy, Mediator, Memento, and Interpreter Design Pattern.
So, these patterns are focused on communication between objects: how they interact and
fulfill their intended purpose. They define clear patterns of communication among objects.
Creational Design Pattern
A Creational Design Pattern focuses on object creation in software development. Instead
of creating objects directly using the new operator (or its equivalent in other languages),
these patterns provide alternative mechanisms to create objects. By abstracting the
object instantiation process, creational patterns make a system independent of how
objects are created, composed, and represented.
The Creational Design Patterns are categorized into two types. They are as follows:
1. Object-Creational Patterns: Object-Creational Patterns deal with object creation.
Here, it defers part of its object creation to another object.
2. Class-Creational Patterns: Class-Creational Patterns deal with class instantiation.
Here, it defers its object creation to subclasses.

Singleton Pattern

Factory Design Pattern


A factory is an object used for creating other objects. A factory is a class with a method.
That method will create and return different objects based on the received input
parameter.

You might also like