Prototype - Creational Design Pattern

Prototype - Creational Design Pattern

2019, Feb 10    

🧱 Prototype

Create object based on an existing object through cloning.

class Computer {
    protected int ramSize;
    protected KeyBoard keyboard;
    protected Mouse mouse;
    protected Motherboard chipset;
    protected CPUCabinet cabinet;
    // assume getters & setters. 
    public Computer clone(){
        Computer that = new Computer();
        // copy all the this content to that content, deeply. 
        return that;
    }
}

When to use? When an object is required that is similar to existing object or when the creation would be expensive as compared to cloning.