Friday, February 27, 2015

Some Design Pattern

Factory Pattern:

Abstract Factory Pattern:called as factory of factories

Singleton Pattern: 


public class SingleObject {
   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();
   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}
   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }
   public void showMessage(){
      System.out.println("Hello World!");
   }

}

Builder Pattern:
Builds a complex object using simple objects and using a step by step approach.
Builder Pattern UML Diagram
Facade Patterns:
pattern hides the complexities of the system and provides an interface to the client using which the client can access the system.

Prototype Pattern:
This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.

Proxy Pattern:
session.load() is the best example for prototype pattern: It will just create a proxy object when its really required then it loads from data base.




No comments: