设计模式 - 原型模式


6. 原型模式(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

作者与出处
原作者:CyC2018
来源:CyC2018
许可证:CC BY-NC-SA 4.0
整理人: 灏天文库整理
由灏天文库结构化整理,提供目录导航、全文检索与在线阅读,便于系统化学习
发布者: 作者: CyC2018 转发
评论区 (0)
U