Prototype design pattern — get the gist in 2 min.

aditya chaudhari
JavaDeveloperDiary — JDD
3 min readMay 9, 2023
prototype design pattern Tshirt product example

Welcome to a world of software development without copy-paste ‼!
Just imagine the nightmare it would be — every time you needed to reuse a piece of code, you would have to manually retype it every single time. Thankfully, we live in a world where copy-paste is a widely accepted practice (without violating the copyright principles ofcource), as programmers, we are always on the lookout for better and more efficient solutions.

here is the Prototype Design Pattern — a pattern that offers a more elegant and flexible alternative to copy-paste. With the Prototype pattern, you can create new objects by cloning existing ones, saving time and reducing the potential for errors.

The Prototype design pattern is a creational pattern that allows you to create new objects by cloning existing ones, rather than by creating them from scratch.

Simple Example
Let’s say we have an online store that sells customized t-shirts. Customers can choose the color, size, and design of the t-shirt.
1. To avoid creating new instances for all the variants we are thinking to
provide a way for the backend to use a base variant and then customize
that base Tshirt and then add that to the cart.
2. Customers can also choose OLD orders, and choose Tshirt from that order as a base variant, modify it and add to the cart.

To keep the brevity adding only main method here please refer entire github code here [github link]

public class Main {
public static void main(String[] args) {
// Create a prototype t-shirt our base default variant
TShirt prototype = new TShirt("red", "M", "logo", 24.99);

// Create an order for 3 t-shirts
//Order order = new Order(prototype, 3);

// Get a clone of the prototype t-shirt
try {

TShirt tShirt = prototype.clone();
// Customize the t-shirt
tShirt.setColor("blue");
tShirt.setSize("L");
tShirt.setDesign("text");
tShirt.setPrice(29.99);

// Add the customized t-shirt to the cart
Cart cart = new Cart();
cart.addItem(tShirt);
System.out.println("\nPrinting items in the cart ");
cart.printItems();


// cloning the product from the OLD order, modify it and then modified product to the cart
// Create a prototype t-shirt our base default variant which belongs to the old order
TShirt oldOrderTSHIRT = new TShirt("black", "L", "Name", 26.99);

// Create an order for 3 t-shirts
Order order = new Order(oldOrderTSHIRT, 3);

// in the getShirt , its providing the cloned object .
TShirt tShirt2 = order.getTShirt();
// Customize the t-shirt
tShirt2.setColor("blue");
tShirt2.setSize("L");
tShirt2.setDesign("Name");
tShirt2.setPrice(26.99);

// adding Tshirt to the cart
cart.addItem(tShirt2);
System.out.println("\nPrinting items in the cart ");
cart.printItems();

} catch (CloneNotSupportedException e) {
e.printStackTrace();
}

}
}
***********************************
OUTPUT
Printing items in the cart
text t-shirt, L, blue: $29.99

Printing items in the cart
text t-shirt, L, blue: $29.99
Name t-shirt, L, blue: $26.99
***********************************

The Prototype pattern is commonly used in situations where many objects with similar properties need to be created quickly and efficiently. This includes scenarios such as creating product catalogs, generating reports, and creating user profiles in a social network.

The Prototype pattern has real-world applicability in scenarios where there are many objects with similar properties that need to be created quickly and efficiently, and where object creation is expensive or time-consuming. The pattern has several benefits, such as reducing the amount of time and resources required to create new objects, promoting code reusability, and allowing for dynamic modification of objects

In Java Cloneable marker interface is provided to clone an object using the prototype principles.
Also if you have come across prototype scope in spring, the prototype scope is based on the Prototype pattern and allows you to define a bean as a prototype, which means that a new instance of the bean will be created each time it is requested.

The real fun is in the details, if you wish to know and understand different styles of strategy pattern and java code examples, pros-cons, please consider visiting my detailed article at [prototype-pattern-java-explained-7-min-read]

Thanks for reading. Love IT, Live IT, Enjoy IT.

On the journey to understand the core essence of design patterns.
Every Tuesday publishes a small 2 min post and a long 5–10 min post along with a code example, please consider following if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT