Monday, December 11, 2017

Prototype Pattern

Definition -

This pattern allows you to clone an object and modify as per your wish and use it for some other operation.
When the objects that you create are complex and it is a costly operation to create one more, then we use this pattern to clone it and modify it.

Scenario -

    Let us take a scenario of building of a car. You are adding all the parts which form a car. Now creating a new object is a costly affair, hence we will clone this and then modify the parts.


Based on the above this is these are the classes which we will create
1. A car class which will keep on adding parts. We will clone the objects and modify it inside the main function.


 
package PrototypePattern;


import java.util.ArrayList;

public class car implements Cloneable {
    
    public ArrayList<parts> carparts;
    
    public car() {
        carparts = new ArrayList<parts>();
    }
    
    public car(ArrayList<parts> part) {
        this.carparts=part;
    }
    
    public void addparts(parts part) {
        carparts.add(part);
    }
    
    public ArrayList<parts> displaycomponents() {
        return carparts;
    }

    
    @Override
    public Object clone() throws CloneNotSupportedException{
        ArrayList<parts> temp=new ArrayList<parts>();
        for(parts p: carparts) {
            temp.add(p);
        }
        
        return new car(temp);
    }

}





2. A parts class which will have attribute called part name.

package PrototypePattern;

public class parts {
    
    String partname;
    
    public parts(String part) {
        this.partname=part;
    }

}





We will first create a class called car1 and add engine and chassis as two components. Then we will clone this into car2 and display the components.

package PrototypePattern;

import java.util.ArrayList;

public class prototypedemo {

    public static void main(String[] args) {
        
        ArrayList<parts> p = new ArrayList<parts>();
        parts engine = new parts("engine");
        p.add(engine);
        parts chassis = new parts("chassis");
        p.add(chassis);
        System.out.println("Creating a new object car");
        car car1=new car(p);
        ArrayList<parts> car1comp= car1.displaycomponents();
        for(parts temp: car1comp) {
            System.out.println(temp.partname);
        }
        
        try {
            car car2=(car) car1.clone();
            
            System.out.println("Cloning..");
            
            ArrayList<parts> car2comp= car2.displaycomponents();
            for(parts temp: car2comp) {
                System.out.println(temp.partname);
            }
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }

    }

}





   


Place to use -
1) When you want to decouple creation and representation of an object.
2) Creating complex object.
3) Creating object which have almost similar attribute. This point is little vague, it depends on the thinking of the designer.


To be careful about -
1) This can be costly affair if this pattern is overused in the system.
2) An object having circular reference cannot use this.

No comments:

Post a Comment