Whats is a Design Pattern?
Design Pattern can be treated as tools used to solve a particular type of problem (mostly a recurring problem over the years). it also proves as a handy shared vocabulary which developers can use while discussing a particular problem at hand.
“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” - Christopher Alexander
Why use a Design Pattern?
They are well tested and refined solutions discovered by software engineers over the years and knowing them can help us disintegrate problems very quickly and efficiently. like for example if we have a scenario where we are required to notify a certain number of class/clients whenever there is an event or change in data of a class, we use Observer Pattern.
Types of Design Patterns
Design Patterns can be classified into three broad categories.
- Creational - what should be the process of object creation in a particular design problem.
- Structural - what should be the structure of classes/objects, helps you design your class compositions while addressing a particular design problem.
- Behavioral - how classes should communicate/interact with each other in order to make a design model work.
I will provide a matrix below which will mention the defination/description,advantages and disadvantages of some of the most commonly used design patterns.
| Pattern | Description | Advantages | Disadvantages |
| Singleton (Creational) |
Provides a model where a class can only have a single instance. |
-gives you control of the instantiation of the object, ensuring that only single object exits of the (singleton) class. -can provide you with a class with global access which can be used for -reduces the load of object creation and destruction. -can act as a service provider for your application or in other words |
-global access can prove in high coupling of the singleton object and potential changes to it can cause a software ripple effect. -unnecessary use of the singleton can result in a performance hit, its -if your singleton object contains locks then it can increase the |
| Adapter (Structural) |
Converts one interface to an other interface, effectively facilitating classes to work together which otherwise cannot due to their incompatible interfaces. |
-code for target and the adaptee(source) does not need any modification, therefore no ripple effects. |
-sometimes adapters can contain too much code and might appear that changes to the actual classes could have been easier, so think wisely before you write an adapter. -adds an additional layer of code and all the conversions between -Might not be able to provide all the functionalities because there can |
| Composite (Structural) |
Facilitates modeling of tree structures which represents part-whole hierarchies . examples could Menus, File Structures, Drawing Objects on a Canvas |
-Constructs part-whole relationship -Easier to add new components which exhibit the same recursive behavior. -Clients can treat each object and compositions uniformily. |
-there is no basic disadvantage of composite pattern. |
| Decorater (Structural) |
this pattern is used to add new behaviors to an existing class to provide easier/comprehensive method calls, effectively wrapping the an existing class to provide additional functionality. (another definition)decorator pattern is used to extend the |
-lets clients the ease of code.
-takes care of the steps that client does not need to focus while using -Decorator pattern can be used instead of Sub classing an object. -Decorate makes use of interfaces and concrete implementations providing |
-Wrapper classes effectively are an extra layer and can decrease efficiency (but the performance hit here can be negligible). |
| Facade (Structural) |
Facade provides a single interface to manage several sub classes effectively simplifying the overall process. |
-makes the communication with a set of applications/classes much easier and neat. |
-whenever there is a new component in the overall process, it requires modification in the facade layer. |
Advantages and Disadvantages of Design Patterns can be debatable from case to case scenarios, a basic rule for a good designer is to analyze the whole problem at hand and draw concrete conclusions on whether a design pattern should be applied or not. Over use of design patterns might result in a significant performance hit, therefore determine tradeoffs wisely.
Cheers.
Dont forget to leave Comments/Feedback.
















