Over a million developers have joined DZone.

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

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

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.


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.


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


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);
        //De-serialization of object
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream in = new ObjectInputStream(bis);
        Shoes clonedShoes = (Shoes) in.readObject();
        return clonedShoes;


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

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


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

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


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

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


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) {
        } catch (ClassNotFoundException e) {
        //Shallow cloning
        Shoes shallowClonedShoes = null;
        try {
            shallowClonedShoes = (Shoes) shoes.shallowCloning();
        } catch (CloneNotSupportedException e) {
        System.out.println("Shallow-Cloned Shoes ========== " +shallowClonedShoes );
        return shallowClonedShoes;


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!

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

java,design pattern,cloning,objects

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

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

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

{{ parent.tldr }}

{{ parent.urlSource.name }}