C# Design Patterns
Design pattern in software engineering is a general, reusable solution to a commonly occurring problem in software design.
Developers face some problems while developing applications or in the software application Lifecycle which are common and repeated, for example, creation and disposal of objects, interaction between objects, the structure of classes that enhance cohesion and loose coupling, fixing of bugs in a way that minimize changing of source codes, etc.
Design patterns are used to solve these commonly occurring problems in the development phase so that we can minimize the problems after deployment. A design pattern suggests a specific implementation for the specific object-oriented programming problem. For example, if you want to ensure that only a single instance of a class exists, then you can use the Singleton design pattern which suggests the best way to create a class that can only have one object.
Design principles are different than design patterns. Learn the difference between design principles and design patterns.
How to use design patterns?
You can use one design pattern or a combination of design patterns to solve a problem in your software application. Each pattern has use cases that suggest the case what a pattern might be a good match.
A pattern might have positive or negative consequences. Implement a pattern when it has more advantages than disadvantages for your problem.
Sometimes two or more design patterns solve one problem. You have to compare then understand their differences before implementing a design pattern.
The 23 Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. The GoF refers to four authors who published a book Design Patterns: Elements of Reusable Object-Oriented Software in 1994 that is still used today.
Types of Design Patterns
Design patterns are categorized into three groups: Creational, Structural, and Behavioral.
Creational Design Patterns
Creational design patterns solve the problems related to object creation. They help to abstract away object creation processes that spread across multiple classes.
The following five patterns are creational design patterns:
- Singleton
- Abstract Factory
- Builder
- Factory Method
- Prototype
Structural Design Patterns
The structural design patterns suggest implementing relationships between classes and objects.
The following seven patterns are structural design patterns:
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
Behavioral Design Pattern
The behavioral design patterns suggest ways of communication between classes and objects.
The following twelve patterns are behavioral design patterns:
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
- Mediator