20 September, 2011

Design Pattern notes

Why do we need design pattern?
Each pattern describe a problem which occurs again  and again and then describe a solution following which we can avoid that problem

Types of design pattern:
1) Creational
2) Structural
3)Behavioral

Creational Design Pattern:
It provides us a system in which the object creation are independent.
a)Factory Pattern:
Provides an interface for creating object but let sub-class decide which class to instantiate. It is used when we require to instantiate a sub-class according to the data provided.
public interface Person {
    public void showPersonDetails(String name);
}
public class Male implements Person{
    @Override
    public void showPersonDetails(String name) {
        System.out.println("hello i am male and my name is="+name);
       
    }
}
public class Female implements Person{
    @Override
    public void showPersonDetails(String name) {
        System.out.println("hi i m female and my name is="+name);
    }
}
public class PersonFactory {
    private PersonFactory(){
       
    }
    public Person getPersonInstance(String gender){
        if(gender.equalsIgnoreCase("male")){
            return new Male();
        }else{
            return new Female();
        }
    }
    public static PersonFactory newInstance(){
        return new PersonFactory();
    }
}
public class FactoryPatternImpl {
    public static void main(String[] args){
        PersonFactory pf = PersonFactory.newInstance();
        Male m =(Male) pf.getPersonInstance("male");
        m.showPersonDetails("mks");
   
    }
}

b)Singleton:
It ensures that a class has only one instance. It is generally used in case of Logging.
public class Log implements Cloneable {
    private static Log log = null;
    private Log(String str) {
        System.out.println("got log instance");
    }
    public synchronized static Log getLogInstance(String str) {
        if (log == null) {
            return new Log(str);
        }
        return log;
    }
    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
}
public class SingletonImpl {
    public static void main(String[] args) {
        Log l1 = Log.getLogInstance("hello");
    }
}


c)Prototype:
The prototype means making a clone. This implies cloning of an object to avoid creation. If the cost of creating a new object is large and creation is resource intensive, we clone the object.
We use the interface Cloneable and call its method clone() to clone the object.



class Car {
    private String name;
    public Car(String s) {
        name = s;
    }
    public String getName() {
        return name;
    }
    public void setName(String s) {
        name = s;
    }
}
 

class Person implements Cloneable {

    private Car car;

    public Car getCar() {
        return car;
    }

    public Person(String carName) {
        car = new Car(carName);
    }

    public Object clone() {
        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }
}



public class PrototypeImpl {
    public static void main(String[] args){
        Person p = new Person("civic");
        Person q = (Person) p.clone();
        System.out.println(q.getCar().getName());
    }
}

d)objectpool:
While the Prototype pattern helps in improving the performance by cloning the objects, the Object Pool pattern offer a mechanism to reuse objects that are expensive to create.


object pool pattern is used in db connection creation.




Structural Design Pattern:
It describes how object and class are composed together to form a larger object.
a)Facade Pattern:
  It hides the complexities of the system and provides an interface to the client from where the client can access the system.
In Java, the interface JDBC can be called a facade. We as
users or clients create connection using the “java.sql.Connection” interface, the implementation of which we are not concerned about. 

In general web application this layer separates the front end layer and service layer.


A general flow of web application:
Front End-->Facade layer-->Service layer-->DAO layer-->DB 




 

No comments:

Post a Comment