Posts

Showing posts with the label Design pattern

Chain of responsibility pattern - Java

State pattern - Java

https://github.com/sujjjith/DS/tree/master/src/main/java/ds/designpattern/state

Decorator pattern - Java

https://github.com/sujjjith/DS/tree/master/src/main/java/ds/designpattern/decorator

Adapter pattern - Java

https://github.com/sujjjith/DS/tree/master/src/main/java/ds/designpattern/adapter

Facade pattern - Java

https://github.com/sujjjith/DS/tree/master/src/main/java/ds/designpattern/facade

Observer pattern - java

https://github.com/sujjjith/DS/tree/master/src/main/java/ds/designpattern/observer

Builder pattern - Java

Image
Guidelines for Builder design pattern in Java 1) Make a static nested class called Builder inside the class whose object will be build by  Builder . In this example its  Cake . 2) Builder class will have exactly same set of fields as original class. 3) Builder class will expose method for adding ingredients e.g.  sugar()  in this example. each method will return same  Builder  object. Builder will be enriched with each method call. 4)  Builder.build()  method will copy all builder field values into actual class and return object of  Item  class. 5) Item class (class for which we are creating Builder) should have  private constructor   to create its object from  build()  method and prevent outsider to access its constructor. public   class  BuilderPatternExample  {        public   static   void  main ( String  args [])   {     ...

Singleton in a cluster - Java

Singleton is a design pattern means allowing only one instance of the class. (Check out more on singleton’s  here ) Singleton works well till the point you have single JVM. In a multiple JVM’s environment, each of them will have their own copy of the singleton object which can lead to multiple issues specially in a clustered environment where the access to the resource needs to be restricted and synchronized. To achieve clustering across JVM’s, one can use multiple techniques (JMS, DB, Custom API, 3rd party tools), but each of them have an impact on the business logic. If the requirement for JVM clustering is realized later in the game, then change to business logic will be huge. In case, the requirement to have singleton across JVM’s is realized later, then tools like Terracotta ,  Oracle Coherenc e are good options. These work on the concept of providing an in memory replication of objects across JVMs in effect providing you singleton view  or making use of any o...

Inversion of Control vs Dependency Injection

IoC is a generic term meaning rather than having the application call the methods in a framework, the framework calls implementations provided by the application. DI is a form of IoC, where implementations are passed into an object through constructors/setters/service look-ups, which the object will 'depend' on in order to behave correctly.

Singleton Class - Java

public class Singleton {     private static Singleton singleton;     //1. private prevents the instantiation of this object from other clesses     private Singleton(){     }     // 2. static word makes the method class level     // 3. synchronized avoids the multiple instantiation of object during multiple concurrent calls     public static synchronized Singleton getSingletonInstance(){         if(singleton == null){             singleton = new Singleton();         }         return singleton;     }     //4. avoiding cloning     public Object clone() throws CloneNotSupportedException{         return new CloneNotSupportedException();     } } public class SingletonDemo {     public static void main(String args[]){       ...