WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Design Patterns · Basic · question 11 of 100

What is the Prototype pattern, and when should you use it?

📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

The Prototype pattern is a creational design pattern that allows an object to create a copy of itself, providing a way to compose new objects from existing ones without exposing their underlying implementation details. The basic idea behind the Prototype pattern is to create new objects by cloning existing ones rather than creating them from scratch, which can be a more efficient way of generating complex objects that require significant processing resources to initialize.

In the Prototype pattern, a prototype interface or abstract class defines the method or methods that can be used to clone the object. Concrete implementations of this interface or abstract class provide the actual implementation details for the cloning operation. When a new object is created, it is created by cloning an existing object through the prototype method.

One of the main benefits of the Prototype pattern is that it allows you to avoid the cost of creating new objects from scratch when you need a large number of similar objects. By copying an existing object, you can save processing time and memory, since you don’t need to initialize a new object entirely.

Another advantage of the Prototype pattern is that it allows you to easily create complex objects that are composed of smaller, simpler objects. By creating a prototype for each of the smaller objects, you can use these prototypes to assemble larger, more complex objects without having to define every aspect of their implementation in one place.

In Java, the Prototype pattern can be implemented using the Cloneable interface and the clone() method. The Cloneable interface does not have any methods, but it serves as a marker interface that indicates that the class implementing it can be cloned. The clone() method is defined in the Object class, but it must be overridden in each concrete class that implements the Cloneable interface in order to define the specific cloning behavior for that class. Here’s an example of how the Prototype pattern can be implemented in Java using the Cloneable interface:

public abstract class Prototype implements Cloneable {
    public abstract Prototype clone();
}

public class ConcretePrototype1 extends Prototype {
    private int field1;

    public ConcretePrototype1(int field1) {
        this.field1 = field1;
    }

    public int getField1() {
        return field1;
    }

    public Prototype clone() {
        return new ConcretePrototype1(this.field1);
    }
}

public class ConcretePrototype2 extends Prototype {
    private String field2;

    public ConcretePrototype2(String field2) {
        this.field2 = field2;
    }

    public String getField2() {
        return field2;
    }

    public Prototype clone() {
        return new ConcretePrototype2(this.field2);
    }
}

In this example, the Prototype abstract class defines a clone() method that must be implemented by any concrete class that extends it. Each concrete class (ConcretePrototype1 and ConcretePrototype2) implements the clone() method to return a new instance of itself with the same field values as the original instance.

To create a new object using a prototype, you would first create an instance of the existing object that you want to clone, and then call its clone() method to create a new object. Here’s an example:

Prototype original = new ConcretePrototype1(42);
Prototype clone = original.clone();

In this example, a new instance of ConcretePrototype1 is created with a value of 42 for the field1 property. Then, the clone() method is called on the original instance to create a new instance of ConcretePrototype1 with the same field1 value.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Design Patterns interview — then scores it.
📞 Practice Design Patterns — free 15 min
📕 Buy this interview preparation book: 100 Design Patterns questions & answers — PDF + EPUB for $5

All 100 Design Patterns questions · All topics