Design patterns are basically the best practices used by expert in software development to achieve high scalability, high maintainability, high solutionability, high flexibility and so on.
Design Patterns can be classified into three categories Creational, Structural and Behavioral patterns.
Creational Design Patterns
It provide a way to create an object while hiding the creation logic instead
of instantiating objects directly using new key word. It provide more
flexibility to the program to decide which objects need to be created for a
given use case.
Example
Factory Pattern, Factory Method Pattern, Abstract Factory Pattern, Singleton Pattern, Builder Pattern, Prototype patterns are the example of creational design pattern.
Structural Design Patterns
Assembling classes and objects into larger structure while keeping the
structures flexible and efficient. Inheritance is an example of this pattern.
Example
Adapter Pattern, Bridge Pattern, Composite Pattern, Decorator Pattern, Facade Pattern, Proxy Pattern are the example of structural design pattern.
Behavioral Design Patterns
This design pattern focus on communication between objects.
Example
Chain of Responsibility Pattern, Observer Pattern, Iterator Pattern and so on are the example of behavioral design patter.
Factory Pattern
Creates objects without exposing the instantiation logic to the client.
Factory Method Pattern
Define an interface for creating an object, but let subclasses decide which
class to instantiate.
Abstract Factory Pattern
Provide an interface for creating families of related or dependent objects
without specifying their concrete classes.
Singleton Pattern
Singleton pattern comes under creational pattern. This pattern ensure that one and only object will be created of a class through out the application. In this pattern a class must have:
- private constructor without any parameter.
- private static instance of itself.
- public static property or method to return this instance outside the class.
- class must be sealed so that can't be inherited.
Example 1 (Not Thread Safe and should be avoid)
Example 2 (Thread Safe with lock but performance will degrade due to lock)
Example 3 (Thread Safe without lock but not quite lazy)
Example 4 (Thread Safe without lock and fully lazy)
Example 5 (using .NET 4's Lazy type)
For details understanding and sample please follow my Github link.
Comments
Post a Comment