首页 > 其他 > 详细

设计模式-原型模式(Prototype)

时间:2020-08-20 09:31:35      阅读:72      评论:0      收藏:0      [点我收藏+]

基本来源于 [CyC2018-设计模式]([https://github.com/CyC2018/CS-Notes/blob/master/notes/设计模式 - 目录.md](https://github.com/CyC2018/CS-Notes/blob/master/notes/设计模式 - 目录.md))

意图(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

设计模式-原型模式(Prototype)

原文:https://www.cnblogs.com/wuminda/p/13533036.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!