A look at the prototype design model

0

This is part of the creation template, which helps to create items as requested. This model uses the philosophy of cloning to create objects, and we know that two methods of cloning are possible. One is shallow cloning and the other is deep cloning. The behavior of the two types of cloning is different, so choose wisely.

You can read more about cloning here.

When to use the prototype model

  1. Whether the creation of objects is complex or expensive.

  2. Whether adding or removing objects is expected at runtime.

  3. If the customer is not aware of the creation of the object.

  4. If an object similar to the existing one is required.

Scenario

When building a banking app, we know that banking transactions require expensive database queries. The transactions would be linked to bank accounts, which can be an individual or a group and with their profiles with the bank. So in this case, once the actual object is created (exactly one object), it may be required in multiple places. The Prototype model can be used to get the copy of this object, which can replace the old object.

PrototypeClient.java

public class PrototypeClient {
    public static void main( String[] args ) {
        Shoes xyz = Factory.makeObject("Sport");
        xyz.wear();
    }
}

Shoes.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map; 

public abstract class Shoes implements Serializable, Cloneable{  
    private static final long serialVersionUID = -1937047519230746677L;
    abstract void wear();
    public Object shallowCloning() throws CloneNotSupportedException    {
        return this.clone();
    }
    public Shoes deepCloning() throws IOException, ClassNotFoundException {
        //Serialization of object
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos);
        out.writeObject(this);
        //De-serialization of object
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bis);
        Shoes clonedShoes = (Shoes) in.readObject();
        return clonedShoes;
    }
}

Sport shoes.java

public class SportShoes extends Shoes {
    private static final long serialVersionUID = 440347043243434494L;
    public String toString() {
        return "SportShoes";
    }

    @Override
    public void wear() {
        //some SportShoes specific code to wear
        System.out.println("I am wearing " + toString());
    }
}

CasualShoes.java

public class CasualShoes extends Shoes {
    private static final long serialVersionUID = -6493171299609719559L;
    public String toString() {
        return "CasualShoes";
    }

    @Override
    public void wear() {
        //some CasualShoes specific code to wear
        System.out.println("I am wearing " + toString());
    }
}

FormalShoes.java

public class FormalShoes extends Shoes {
    private static final long serialVersionUID = -5670125744755511170L;
    public String toString() {
        return "FormalShoes";
    }

    @Override
    public void wear() {
        //some FormalShoes specific code to wear
        System.out.println("I am wearing " + toString());
    }
}

Factory.java

public class Factory {
    private static Map prototypes = new HashMap();
    static {
        prototypes.put( "Sport",   new SportShoes() );
        prototypes.put( "Casual",  new CasualShoes() );
        prototypes.put( "Formal", new FormalShoes() );
    }
    public static Shoes makeObject(String shoesType) {
        Shoes shoes = (Shoes)prototypes.get(shoesType);
        //deep cloning (using in-memory)
        try {
            Shoes deepClonedShoes = shoes.deepCloning();
            System.out.println("Deep-Cloned Shoes ========== "+deepClonedShoes);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //Shallow cloning
        Shoes shallowClonedShoes = null;
        try {
            shallowClonedShoes = (Shoes) shoes.shallowCloning();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println("Shallow-Cloned Shoes ========== " +shallowClonedShoes );
        return shallowClonedShoes;
    }
}

Go out:

Deep-Cloned Shoes ========== SportShoes
Shallow-Cloned Shoes ========== SportShoes
I am wearing SportShoes

Hope this creates a clear understanding of the prototype design pattern and its implementation. Good learning!


Source link

Share.

Leave A Reply