Tutorials Design Patterns Prototype Design Pattern In Java

Prototype Design Pattern In Java

Over the course of this article, we will examine Prototype design pattern in Java. Prototype design pattern belongs to the creational family of pattern. It is used when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. In a simpler term, using prototype pattern, we will create new instances through cloning existing instances.

Creating instance in java is one of the costly operation. It costs time and resources. If there is a scope to avoid object creation process, by just copying the existing object, then won’t it be great? Prototype design patterns answers that problem.

Use the prototype design pattern when,

  1. When specifying new objects by changing an existing objects structure
  2. Speeds up instantiation of large, dynamically loaded classes

Prototype Design Pattern Example

The Hampstead Cake company are well known for providing quality birthday cheese cakes in London. For each order the cheese cake has to be customized with recipients name, however the ingredients and taste remains the same. The cake company is getting inundated orders and now struggling to deliver them. The trouble is, it is taking too long to specify ingredients and build cheese cake for each order. That way they cannot produce enough cakes. How about you just produce a single cheesecake, and clone it multiple times, and then customize each one?

In this example, CakeStore class is the client. The client creates multiple prototypical  instances by calling prepareCake() method.

Prototype Design Pattern


package com.javatechig.designpattern.prototype;

public interface Cake extends Cloneable {

	public Cake prepareCake();


package com.javatechig.designpattern.prototype;

public class CheeseCake implements Cake {

	private String sugar;
	private String butter;
	private String cheese;
	private String name;

	public Cake prepareCake() {
		Cake cake = null;
		try {
			cake = (Cake) super.clone();
		} catch (CloneNotSupportedException e) {
		return cake;

	void addSugar(String sugar) {
		this.sugar = sugar;

	void addButter(String butter) {
		this.butter = butter;

	public void addCheese(String cheese) {
		this.cheese = cheese;

	public void setName(String name) {
		this.name = name;

	public String toString() {
		return "Sugar:" + sugar + ", Butter:" + butter + ", Name:" + name
				+ ", Cheese:" + cheese;



package com.javatechig.designpattern.prototype;

public class CakeStore {

	public static void main(String[] args) {

		/* Cheese cake preparation */
		CheeseCake cake = new CheeseCake();
		System.out.println("Cake = " + cake.toString());

		/* Order with custom name */
		CheeseCake cake1 = (CheeseCake) cake.prepareCake();
		System.out.println("Cake1 = " + cake1.toString());

		/* Order with custom name and customized cheese */
		CheeseCake cake2 = (CheeseCake) cake.prepareCake();
		cake2.addCheese("Extra Cheese");
		System.out.println("Cake3 = " + cake2.toString());


Cake = Sugar:100g, Butter:200g, Name:null, Cheese:Acapella
Cake1 = Sugar:100g, Butter:200g, Name:Bastien, Cheese:Acapella
Cake3 = Sugar:100g, Butter:200g, Name:Adams, Cheese:Extra Cheese
Inline Feedbacks
View all comments
Previous Post
Java Design Pattern Tutorials

Adapter Design Pattern In Java

Next Post
Java Design Pattern Tutorials

Builder Design Pattern In Java

Related Posts

State Design Pattern in Java

Over the course of this article, we will examine State design pattern in java with help of realtime examples. The State design pattern belongs to the behavioral family of pattern that deals with the runtime object behavior based on the current state. The definition of State Design Pattern as per the original Gang of Four book is; "Allows an object to alter its behavior when its internal state changes. The object will appear to change its class".
Read more
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
Would love your thoughts, please comment.x