Prototype Design Pattern In Java

By | | Updated : 2022-02-12 | Viewed : 3142 times

Prototype Design Pattern In Java

This current tutorial teaches us about the Prototype Design pattern. We are going to learn about usage, advantages, and example that show how the pattern is.

Prototype Design Pattern

In our application, we have to create very complex objects in which contain many resources which contain costly operations. So these resources are also very costly in terms of creation in runtime. It is better to copy/clone than create. The same concept applies here to that Prototype Design Pattern.

The Prototype Design Pattern should be allowed to use if the cost of an object is too expensive. One of the creational design patterns clones the existing object rather than creating the entire object. And Once it is cloned we can change the required resources in that newly created object. Notice how it is very useful in complex object creation where Object creation is the time consumed.

When To Use Prototype Design Pattern

Prototype Design Pattern must be used when object creation is very complex and time consumed. Prototype Design Pattern creates that complex object with costly resources in runtime. So it is useful for objects where objects will be created in Runtime. The usage of classes (resources) will be reduced as we are cloning the existing object.

Prototype Design Pattern Example In Java

Now It is time to implement the Prototype design pattern example. In Prototype implementation, we need to implement Laptop and LaptopBuilder classes.

Code Laptop Class with LaptopBuilder

Notice, in the below given example, the Laptop class contains is LaptopBuilder as a static class.
package com.docsconsole.tutorials.prototype;

public class Laptop {

    private final String processor;
    private final Integer ram;
    private final String storage;
    private final Integer capacity;

    public Laptop(LaptopBuilder laptopBuilder) {
        this.processor = laptopBuilder.processor;
        this.ram = laptopBuilder.ram; =;
        this.capacity = laptopBuilder.capacity;


    public final static class LaptopBuilder {

        private String processor;
        private Integer ram;
        private String storage;
        private Integer capacity;

        public LaptopBuilder processor(String processor) {
            this.processor = processor;
            return this;

        public LaptopBuilder ram(Integer ram) {
            this.ram = ram;
            return this;

        public LaptopBuilder storage(String storage) {
   = storage;
            return this;

        public LaptopBuilder capacity(Integer capacity) {
            this.capacity = capacity;
            return this;

        public Laptop build() {
            return new Laptop(this);

    public String toString() {
        return "Laptop{" +
                "processor=\'" + processor + \'\\'\' +
                ", ram=" + ram +"GB"+
                ", storage=\'" + storage + "\\'" +
                ", capacity=" + capacity +"GB"+

Code LaptopBuilderDemo Class

Now we will write the builder demo class for the Laptop and its LaptopBuilder.
package com.docsconsole.tutorials.prototype;

public class LaptopBuilderDemo {
    public static void main(String[] args) {
        Laptop laptop = new Laptop.LaptopBuilder().processor("Intel Core I5").ram(4).storage("SSD").capacity(500).build();
Leave A Reply