Prototype

- 7 mins

Build Status

/assets/images/UML/DesignPatterns/prototype.png

“CupPrototype” represents the prototype object we will clone.

public abstract class CupPrototype implements Cloneable{
    private int id;
    protected double capacity;
    protected String material;
    protected String type;

    public CupPrototype(int id, double capacity, String material) {
        this.id = id;
        this.capacity = capacity;
        this.material = material;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getCapacity() {
        return capacity;
    }

    public void setCapacity(double capacity) {
        this.capacity = capacity;
    }

    public String getMaterial() {
        return material;
    }

    public void setMaterial(String material) {
        this.material = material;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @Override
    public String toString() {
        return "CupPrototype{" +
                "id='" + id + '\'' +
                ", capacity=" + capacity +
                ", material='" + material + '\'' +
                ", type='" + type + '\'' +
                '}';
    }

    public Object clone() {
        Object clone = null;

        try {
            clone = super.clone();

        } catch (CloneNotSupportedException e) {
            System.out.println("Problem when cloning the object: " + e.getMessage());
            e.printStackTrace();
        }
        return clone;
    }
}

Our other 3 objects extending the prototype object.

public class WaterCup extends CupPrototype{

    public WaterCup(int id, double capacity, String material) {
        super(id, capacity, material);
        type="WaterCup";
    }

    @Override
    public String toString() {
        return "WaterCup{" +
                "capacity=" + capacity +
                ", material='" + material + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
public class TeaCup extends CupPrototype {
    private String accessory;
    
    public TeaCup(int id, double capacity, String material,String accessory) {
        super(id, capacity, material);
        type="TeaCup";
        this.accessory=accessory;
    }

    public String getAccessory() {
        return accessory;
    }

    public void setAccessory(String accessory) {
        this.accessory = accessory;
    }

    @Override
    public String toString() {
        return "TeaCup{" +
                "accessory='" + accessory + '\'' +
                ", capacity=" + capacity +
                ", material='" + material + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}
public class MeasuringCup extends CupPrototype {
    private String ratingScale;
    
    public MeasuringCup(int id, double capacity, String material,String ratingScale) {
        super(id, capacity, material);
        type="MeasuringCup";
        this.ratingScale=ratingScale;
    }

    public String getRatingScale() {
        return ratingScale;
    }

    public void setRatingScale(String ratingScale) {
        this.ratingScale = ratingScale;
    }

    @Override
    public String toString() {
        return "MeasuringCup{" +
                "ratingScale='" + ratingScale + '\'' +
                ", capacity=" + capacity +
                ", material='" + material + '\'' +
                ", type='" + type + '\'' +
                '}';
    }
}

Finally, we write our test object.

public class Client {
    public static void main(String[] args) {
        TeaCup slenderTeaCup = new TeaCup(1, 100, "glass", "tea plate");
        System.out.println(slenderTeaCup);

        TeaCup cup = (TeaCup) slenderTeaCup.clone();
        cup.setId(2);
        cup.setAccessory("handle");
        System.out.println(cup);
    }
}

Code output:

TeaCup{accessory='tea plate', capacity=100.0, material='glass', type='TeaCup'}
TeaCup{accessory='handle', capacity=100.0, material='glass', type='TeaCup'}

You can find the code in this article on my GitHub page.

« Design Patterns

comments powered by Disqus
Uğur Özalp

Uğur Özalp

A man traveling the world (not now because I'm staying at home), photographing and encoding

rss twitter github gitlab youtube mail spotify lastfm facebook instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora