Java design pattern: Prototype

Today we will take a look at another Java design pattern from the category of patterns for creating objects (creational patterns) – Prototype.

Read more – a series of design patterns:

Čo to je návrhový vzor Prototype?

Design pattern Prototype is a software design pattern that is used to create new objects by copying an existing object.

What problem does the Prototype design pattern solve?

It solves the problem of creating new objects with minimal cost in computational time and memory by avoiding the repetitive creation of objects using the constructor. Instead, we create one sample instance (prototype) that we can then copy when we need to create a new object.

This design pattern is useful when object creation is difficult or when getting new instances of an object is different from simply calling the constructor. It is also useful for creating deep copies of objects that contain complex data structures.

Example of Prototype design pattern implementation in Java

Now we will show how to implement the Prototype pattern in Java.

Tvar.java

The shape class represents a prototype pattern that must implement the Cloneable interface and overload the clone() method.

package designpatterns;

public class Tvar implements Cloneable {

    private String typ;

    public Tvar(String typ) {
        this.typ = typ;
    }

    public String getTyp() {
        return typ;
    }

    public void setTyp(String typ) {
        this.typ = typ;
    }

    @Override
    public Tvar clone() throws CloneNotSupportedException {
        return (Tvar) super.clone();
    }
}

Here is an example of using the Shape class with the Prototype design pattern implementation:

Main.java

import designpatterns.Tvar;

public class Main {
    public static void main(String[] args) {
        // Vytvorenie vzoru
        Tvar prototype = new Tvar("Kruh");

        try {
            // Okopirovanie vzoru pre vytvorenie noveho objektu
            Tvar tvar1 = prototype.clone();
            System.out.println("Objekt 1: " + tvar1.getTyp());

            // Zmena typu noveho objektu
            tvar1.setTyp("Štvorec");
            System.out.println("Objekt 1 po zmene typu: " + tvar1.getTyp());

            // Vytvorenie dalsieho objektu okopirovanim vzoru
            Tvar tvar2 = prototype.clone();
            System.out.println("Objekt 2: " + tvar2.getTyp());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

Here is an example of using the Tvar class with the Prototype design pattern implementation: In the main method, we first created a prototype instance with type Kruh. We then copied this instance and created a tvar1 object. After changing the tvar1 type, we copied the pattern again and created the tvar2 object.

The output of this example should be:

java output for prototype design pattern
output of the example

We have prepared files with the above mentioned example Prototype code that you can run directly in Java. Download the code here.

 

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you