原型模式(Prototype)

Intent

使用原型实例指定要创建对象的类型,通过复制这个原型来创建新对象。

Class Diagram

 

Implementation

public abstract class Prototype {
    abstract Prototype myClone();
}
public class ConcretePrototype extends Prototype {

    private String filed;

    public ConcretePrototype(String filed) {
        this.filed = filed;
    }

    @Override
    Prototype myClone() {
        return new ConcretePrototype(filed);
    }

    @Override
    public String toString() {
        return filed;
    }
}
public class Client {
    public static void main(String[] args) {
        Prototype prototype = new ConcretePrototype("abc");
        Prototype clone = prototype.myClone();
        System.out.println(clone.toString());
    }
}
abc

JDK

  • java.lang.Object#clone()
posted @ 2018-11-13 23:06  青春暮年  阅读(104)  评论(0编辑  收藏  举报