Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A Look at the Prototype Design Pattern

DZone's Guide to

A Look at the Prototype Design Pattern

Dive into the Prototype pattern and learn how, with the help of some cloning, you can ease your transaction costs as well as share a few other benefits.

· Java Zone
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

This is a part of the Creational pattern, which helps to create objects as requested. This pattern 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 both cloning types is different, so choose wisely.

You can learn more about cloning here.

When to Use the Prototype Pattern

  1. If the creation of objects is complex or costly.

  2. If the addition or removal of objects is expected on runtime.

  3. If the client should be unaware of the object creation.

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

Scenario

When creating a bank application, we know that bank transactions need expensive database queries. Transactions would be linked with bank accounts, which may be an individual or group and with their profiles with the bank. So, in this case, once the actual object is created (exactly one single object), it may be required further in a number of places. The Prototype pattern can be used to get the copy of that 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;
    }
}

SportShoes.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<String, Object> prototypes = new HashMap<String, Object>();
    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;
    }
}

Output:

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

I hope this creates a clear understanding about the Prototype design pattern and how to implement it. Happy Learning!

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:
java ,design pattern ,cloning ,objects

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}