Prototype pattern in Java

public interface Prototype {
    
   public Object cloneObject();
}


public class ConcretePrototype implements Prototype {

    @Override
    public Object cloneObject() {
        // TODO Auto-generated method stub
        return new ConcretePrototype();
    }
}

public class Client {
    
    private Prototype prototype;
    
    public Client(Prototype prototype) {
        this.prototype = prototype;
    }
    
    public Prototype getPrototype() {
        return prototype;
    }



    public void setPrototype(Prototype prototype) {
        this.prototype = prototype;
    }

    public static void main(String[] args) {
        Client c = new Client(new ConcretePrototype());
        c.getPrototype().cloneObject();
    }
}

Highly recommended blog for learning design patterns: java_my_life

Code address: lennon

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325446343&siteId=291194637