Design patterns, as name suggest, are solutions for most commonly (and frequently) occurred problems while designing a software. These patterns are mostly “evolved” rather than “discovered”. A lot of learning, by lots of professional, have been summarized into these design patterns. None of these patterns force you anything in regard to implementation; they are just guidelines to solve a particular problem – in a particular way – in particular contexts. Code implementation is your responsibility.
Being so much of importance, let’s learn these design patterns (in context of java) in more detail.
Creational Design Patterns
Creational patterns often used in place of direct instantiation with constructors. They make the creation process more adaptable and dynamic. In particular, they can provide a great deal of flexibility about which objects are created, how those objects are created, and how they are initialized.
|Builder||This is an alternative way to construct complex objects and should be used only when you want to build different immutable objects using same object building process.|
|Prototype||This is used in scenarios where application needs to create a number of instances of a class, which has almost same state or differs very little.|
|Factory||This is most suitable where there is some complex object creation steps are involved. To ensure that these steps are centralized and not exposed to composing classes, factory pattern should be used.|
|Abstract factory||Whenever you need another level of abstraction over a group of factories, you should consider using abstract factory pattern.|
|Singleton||When an application wants to have one and only one instance of any class per JVM, in all possible scenarios without any exceptional condition.|
Structural Design Patterns
These design patterns show you how to glue different pieces of a system together in a flexible and extensible fashion. Structural patterns help you guarantee that when one of the parts changes, the entire structure does not need to change.
|Adapter||Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.|
|Decorator||This is used to add additional features or behaviors to a particular instance of a class, while not modifying the other instances of same class.|
Behavioral Design Patterns
A behavioral pattern abstracts an action you want to take from the object or class that takes the action. By changing the object or class, you can change the algorithm used, the objects affected, or the behavior, while still retaining the same basic interface for client classes.
|Chain of responsibility||This gives more than one object an opportunity to handle a request by linking receiving objects together.|
|Visitor||When you want a hierarchy of objects to modify their behavior but without modifying their source code.|
Happy Learning !!