What are the Design Patterns?
- Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.
- Design patterns are the templates provided in order to solve the programming problem.
- They are generally used to solve the object creation and its integration
- When user is working on some project, he/she knows the requirements of the clients. So user selects the design pattern and use it for the project.
Types of Design Patterns
The design patterns are classified as:
1. Creational Design Patterns
- The Creational Design Pattern helps the programmer to decide which object or group of objects need to be created by understanding the project scope and requirements.
- It only involves the object instantiation login and nothing other than this like implementation is not involved in this design pattern.
- It generally involves creation and instantiation of object.
- Creational design pattern involve different types of sub-categories which are as follows:
- Factory Method design pattern: It is used to create the objects of sub-classes. For example, programmer has one base class and 2 derived classes and he wants to create and return the object of derived class, factory method pattern.
- Builder design pattern: Builder design pattern is used when user creates a complex object using multiple simple/generic objects. It separate the construction from representation
- Prototype design pattern: It creates a new object from existing instance of object. User can clone the existing object/model into new object and the changes performed by user into the new object won’t affect the original/existing object.
- Singleton design pattern: In this type of pattern, only one intake of class is created and entire application uses the same object to perform different operations.
2. Structural Design Patterns
- In order to maintain the structure of classes and interfaces, structural design patterns are used.
- For example, we have two classes in our project namely Student and Books and Book class is defined within the Student class and they have one to many relationships between them. But now as client’s requirement changes programmer want Student and Book class should be independent of each other. So this is call structural change which affects the project. And here structural design pattern helps us.
- To maintain the relationship between classes and interfaces, structural design pattern is used.
- Adapter design pattern: Two independent or incompatible classes/objects can work together with the help of Adapter class. And this is how adapter class helps the user to perform communication between two independent classes.
- Bridge design pattern: In this design pattern, two terms namely abstraction and implementation are involved. And these two terms are not dependent on each other. The change in one won’t affect the other part. And here client can access only abstraction part only.
- Composite design pattern: In Composite, the group of objects are represented using a tree structure where each node represents an object. In Composite design pattern, all group of objects are treated with similar way as a single object.
- Decorator design pattern: The Decorator is used to add the feature/functionality to an object without affecting the existing functionalities. It is used to dynamically add functionalities at runtime without disturbing the existing functionality.
- Façade design pattern: It provides an interface to the client which is easy-to-use and through which client can access the system easily.
- Flyweight design pattern: This design pattern is used where there is needed to create a large number of objects which are of similar type. As user creates the large number of objects, eventually the memory required for those objects is also large. So here flyweight is used which decreases the number of objects of similar type and eventually reduces the size of memory required.
- Proxy design pattern: It is termed as an object which is used to access real-time object behind the scene. It is act as third party object or interface in order to access the original object or class.
3. Behavioral Design Patterns
- To define the communication between interfaces, classes and objects, behavioral design patterns are used.
- If you change the definition of an interface or class, then that change should be reflected to the other parts of program as well.
- For example, Product is one class in project and later on programmer add some other properties to the Product class so this is called behavioral change and here behavioral design patterns helps us to do such changes without disturbing the project flow.
- Following are the sub-categories of Behavioral design pattern:
- Chain of Responsibility design pattern: As its name suggests, it creates the chain of receiver objects. And if the first receiver is not able to handle request, the request is sent to the next receiver. Here, a chain is developed and request is sent from one receiver to the next and it continues further.
- Command design pattern: In Command design pattern, a request is encapsulated as an object in form of command, then this command is sent to invoker then the invoker finds an appropriate object which can handle/execute the command.
- Iterator design pattern: In Iterator design pattern, user can access the elements of the aggregate object without knowing the internal representation of the object. And access of elements is sequential, it means user can access the elements sequentially.
- Mediator design pattern: In mediator design pattern, a mediator object is defined and this mediator object is responsible for the communication between different objects. Here, the mediator object defines the loose coupling which means the interaction between the objects is loosely coupled that is less interaction between the objects so that the change in one object won’t affect the other object that much.
- Memento design pattern: This pattern is used to perform the rollback or undo type of operation. It stores the internal state of an object.
- Observer design pattern: In Observer design pattern, there is one-to-many relationship between the objects. In one-to-many relationship, one (called as Subject) and many (called as Dependents) and one and many are nothing but the one object and many objects. So if there some changes occurs in one object, all its dependents get notification about the change and get updated automatically. It is also knows as Dependent design pattern.
- State design pattern: Using State design pattern, an object can its behavior depending on its internal state. It means that this design pattern help user to change the state of an object and eventually its behavior also get changed.
- Strategy design pattern: In Strategy design pattern, to solve one problem set of solution are made. And at runtime, one solution is selected out of those set of solutions. This design pattern is also known as Policy design pattern.
- Template method design pattern: In template design pattern, an algorithm is defined which contains the sequence of steps. And template method allows subclasses to redefine some steps of the algorithm without changing the structure/sequence of an algorithm.
- Visitor design pattern: Visitor object is used in visitor design pattern. Visitor object changes the algorithm of an object. Hence, the algorithm element object varies as the visitor object varies.
Design pattern play an important role while solving any development problem. Design pattern provides away to approach a problem. In C#, there are three types of design patterns are available and these patterns are also classified further into sub categories. Programmer has to identify this definition and needs to understand all design patterns they have to take a design pattern and apply it to the solution. This is how programmer can use these design patterns in real time to solve the problem. To summarize an entire thing we can say that design patterns are important code snippets that are reusable and solves the problem. Hence, C# developers look at the design patterns as an important thing as C# is dynamic programming language.