Thursday, 30 October 2014

Prototype Design Pattern


Instead of creating new object every time, Prototype pattern creates an object using cloning. If the cost of creating a new object is large and creation is resource intensive, it is preferable to clone the object.

Using Prototype design pattern, we clone the existing instance in hand and then make the required changes to the cloned instance.

Car.java
public abstract class Car implements Cloneable{
   private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void print(){
        
    }

    @Override
    public Car clone(){
        Car myCar = null;
        try {
            myCar = (Car)super.clone();
        }
        catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return myCar;
    }
}

SportsCar.java
public class SportsCar extends Car{
    SportsCar(){
        this.setType("Sports Car");
    }

    @Override
    public void print(){
        System.out.println("Car of type :" + this.getType());
    }
}

LuxuryCar.java
public class LuxuryCar extends Car{
    LuxuryCar(){
        this.setType("Luxury Car");
    }

    @Override
    public void print(){
        System.out.println("Car of type :" + this.getType());
    }
}

LargeCar.java
public class LargeCar extends Car{
    LargeCar(){
        this.setType("Large Car");
    }

    @Override
    public void print(){
        System.out.println("Car of type :" + this.getType());
    }
}


MidSizeCar.java
public class MidSizeCar extends Car{
    MidSizeCar(){
        this.setType("Mid Size Car");
    }

    @Override
    public void print(){
        System.out.println("Car of type :" + this.getType());
    }
}


SmallCar.java
public class SmallCar extends Car{
    SmallCar(){
        this.setType("Small Car");
    }

    @Override
    public void print(){
        System.out.println("Car of type :" + this.getType());
    }
} 
 


CarsPrototypes.java

import java.util.Map;
import java.util.HashMap;

public class CarsPrototypes {
    private static Map<Integer, Car> carsCache;
    
    public static void loadCache(){
        Car sportsCar = new SportsCar();
        Car luxuryCar = new LuxuryCar();
        Car largeCar = new LargeCar();
        Car midSizeCar = new MidSizeCar();
        Car smallCar = new SmallCar();

        carsCache = new HashMap<Integer, Car> ();

        carsCache.put(1, sportsCar);
        carsCache.put(2, luxuryCar);
        carsCache.put(3, largeCar);
        carsCache.put(4, midSizeCar);
        carsCache.put(5, smallCar);
    }

    public static Car getCar(int carId){
        Car myCar = carsCache.get(carId);
        return myCar.clone();
    }
}

PrototypePatternDemo.java
public class PrototypePatternDemo {
    public static void main(String args[]){
        Car myCar;
        CarsPrototypes.loadCache();

        myCar = CarsPrototypes.getCar(1);
        myCar.print();

        myCar = CarsPrototypes.getCar(2);
        myCar.print();

        myCar = CarsPrototypes.getCar(3);
        myCar.print();

        myCar = CarsPrototypes.getCar(4);
        myCar.print();

        myCar = CarsPrototypes.getCar(5);
        myCar.print();
    }
}


Output
Car of type :Sports Car
Car of type :Luxury Car
Car of type :Large Car
Car of type :Mid Size Car
Car of type :Small Car


No comments:

Post a Comment