Introduction :
In this article, I discussed Design Patterns in C# With Examples, Design patterns provide general solutions and flexible way to solve common design problems. Patterns are about reusable designs and interactions of objects. Writing the code with design patterns will make your applications more Scalable,Reliable,and Maintainable.
Before starting with design patters in .NET, will understand what is meant by design patterns and why it is useful in software programming
What are Design Patterns in Software Development ?
Design Patterns is a reusable solution to common software design problems that occur repeatedly in real-world application development. It is a template or description of how to solve problems that can be used in many situations.
Design patterns are the most powerful tool for software developer. It is important to understand design patterns rather than memorizing its classes, methods and properties. It is also important to learn how to apply pattern to specific problem to get the desired result. Patterns are used by developers for their specific designs to solve their problems. Pattern choice and usage among various design patterns depends on individual needs and problems.
Types of Design Patterns
Design Patterns in C# are grouped into three main categories.
- Creational Design Pattern
- Structural Design Pattern
- Behavioral Design Pattern
Creational Design Patterns:
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, If we have a big project with a lot of classes, So you need to create different objects of these class If these objects creations are scattered on the client code, then it leads to lots of complecity in code. If the object creations are not centralized then it leads to very complicated code. The Creational Design pattern centralized the object creation logic.
Example :
EmployeeRecord Em=new EmployeeRecord ();
As the name says Creational design patterns deal with object creation and initialization. Creational design pattern gives the programmer more flexibility in deciding which objects need to be created for a given case.
There are following 6 types of creational design patterns:
- Singleton
- Factory
- Builder
- Prototype
- Factory Method
- and Abstract Factory
Structural Design Patterns :
These design patterns are about organizing different classes and objects to form larger structures and provide new functionality.
Sometimes you need to change the structure of a class or you can say the relationship between the classes but you don’t want the project to be affected. This is where the Structural Design Pattern helps.
Examples of Structural design patterns category:
- Adapter
- Facade
- Decorator
- Composite
- Proxy
- Flyweight
- Bridge
Behavioral Design Patterns:
Behavioral design patterns are concerned with the interaction and responsibility of objects.
In these design patterns, the interaction between the objects should be in such a way that they can easily communicate to each other and still should be loosely coupled.so we can say These patterns deal with communication between Classes and objects
Examples of Behavioral design patterns:
- Command
- Observer
- Iterator
- Chain of Responsibility
- Memento
- Mediator
- Visitor
- Template Method
- State
- Interpreter
it is very important for you to have at least the basic knowledge of the following object-oriented programming concepts for easily understanding for Design Patterns in C#.
- C# Polymorphism with Examples
- C# Inheritance with Examples
- What is Encapsulation in C#
- C# Abstraction with Examples
- C#-Interface with Example