The Prototype Pattern

Daniel Boyce
4 min readDec 12, 2023

--

Introduction

The Prototype Pattern ( TPP ) is another creational design pattern. It aims to solve the problem of creating new objects by copying an existing instance. It allows the creation of new objects by duplicating an existing object.

The Problem

Sometimes creating new objects can be expensive, especially if it involves complex initialization. TPP helps us avoid this overhead by copying existing objects. TPP solves this problem by defining an interface or abstract class that declares a method for cloning itself. Then Concrete classes can implement this interface and provide their own logic for cloning.

Example

Below we have a simple java program. ( Using TPP on a simple object could be considered overkill as TPP is only a necessity for more complex objects but the example is simple in order to focus on implementation of the pattern. )

We have a class that creates simple objects that hold an integer. Without using TPP you can see below how we might go about creating two instances of the SimpleObject.

class SimpleObject {
public int number;

public SimpleObject(int number) {
this.number = number;
}

public int getNumber() {
return number;
}
}

public class Main {
public static void main(String[] args) {
SimpleObject original = new SimpleObject(10);

SimpleObject copied = new SimpleObject(original.getNumber());

System.out.println("Original number: " + original.getNumber());
System.out.println("Copied number: " + copied.getNumber());

copied = new SimpleObject(20);

System.out.println("Original number: " + original.getNumber());
System.out.println("Copied number after modification: " + copied.getNumber());
}
}

Now we can take a look at how to implement the same class but with TPP. Below we are now using an interface called Prototype with two methods clone and getNumber. Our SimpleObject class will now implement the Prototype interface.

In a complex program the cloning method will look a lot different, this is as basic as it gets with a simple return new SimpleObject(this.number).

As you can see in the Main class, after the copy is instantiated, we can alter the data inside the copy and the original remains the same. Read on to find out about the idea of a shallow or deep copy and decide if this example is an example of a shallow or deep copy.

interface Prototype {
Prototype clone();
int getNumber();
}


class SimpleObject implements Prototype {
private int number;

public SimpleObject(int number) {
this.number = number;
}

@Override
public Prototype clone() {
return new SimpleObject(this.number);
}

public int getNumber() {
return number;
}

public void setNumber(int num) {
this.number = num;
}
}


public class Main {
public static void main(String[] args) {
Prototype original = new SimpleObject(10);

Prototype copied = original.clone();

System.out.println("Original number: " + original.getNumber());
System.out.println("Copied number: " + copied.getNumber());

((SimpleObject) copied).setNumber(20);

System.out.println("Original number: " + original.getNumber());
System.out.println("Copied number after modification: " + copied.getNumber());
}
}

Benefits

Some of the benefits of implementing TPP may include :

Reduced overhead : Avoids the need for complex initialization by copying existing objects.

Flexibility : Allows for the dynamic creation of objects at runtime.

Simplified object creation : Encapsulates the object creation process within the prototype which makes it easier to manage.

Tradeoffs

The main tradeoff for using TPP would be :

Complexity : Managing the cloning process, especially for complex objects. TPP might introduce more complexity to your program.

Other considerations

State handling

TPP deals with how object states are handled during cloning. Depending on what is needed, the cloned object might share the same state as the original or may require different state independent from the original.

Difference between a shallow and a deep copy of a clone

Shallow copy

In a shallow copy, the cloning process creates a new object but copies the references to the objects within the original object. So changes made to referenced objects within the cloned object will reflect in both the original and cloned objects if they share the same reference.

Deep copy

In a deep copy, the cloning process creates a new object and copies all the objects referenced within the original object. This means that the deep copy is completely independent of the original so if changes are made in the copy this will not affect the original object.

Sources -

Vivek Balasubramaniam, https://www.baeldung.com/java-pattern-prototype

Java Design Patterns & Solid Design Principles, Udemy by Coffee Powered Crew

https://www.udemy.com/course/design-patterns-in-java-concepts-hands-on-projects/

Head First Design Patterns by Freeman and Robson

*This article is intended for my own practice and revision, if someone reading this spots an error please leave a message and I will amend it.

--

--

Daniel Boyce

Interested in all things tech. I'm using this platform mainly as a place to store things that I think I will want to revise in the future