Design Patterns

An anti-pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive ( wikipedia). 

Strategy :  This pattern allows selecting an algorithm/concrete implementation at runtime.  Decouples implementation/algorithm from the context/client.   Normally Strategy pattern is used in combination with Factory patten.

Initialization On Demand Holder : This pattern takes care of thread-safety issues in singleton.  In Singleton, unless we synchronize the part where you create the instance, there is still a chance that multiple instance might get created.   See the example code in Reference below

Builder: This pattern is used for complex objects creations.  This decouples the complex object creation process from the object representation.

Chain of Responsibility ( COR ) or Chain of Command:  This pattern allows breaking down a complex operation into a sequence of small steps, thus making  the code simpler in the process.

Template Method:  Java provides abstract methods for this purpose.  This pattern defines a method template which can be implemented later in various ways.

Observer : One to many ( subject ->observers).  Used for modeling Publish-Subscribe scenarios. So there is a scenario where change in state of one object needs to be notified to multiple objects, this pattern can be used.  This will help keeping the code clean.

Adapter : When you need to use objects of  one interface and fit it into some other interface, this pattern can be used. Very good example is the phone adaptors for various types of  outlets.  Adaptor pattern allows two incompatible interfaces to work together.

State : This patten encapsulates various states of an entity and how it transitions from one state to another.

Reference : http://www.thedevpiece.com/design-patterns-that-every-developer-should-know/

Comments

Popular posts from this blog

SQL

Analytics

DBeaver