原型模型 Prototype

作者: Tim在路上 | 来源:发表于2018-10-27 13:15 被阅读3次

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

    在原型的基础上进行开发

    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());
        }
    }
    

    创建一个对其构造函数的拷贝的函数。

    相关文章

      网友评论

        本文标题:原型模型 Prototype

        本文链接:https://www.haomeiwen.com/subject/mkfmaftx.html