原型
2023-07-06 16:58:36 9 举报
原型
作者其他创作
大纲/内容
public class Client{ public static void main(String[] args){ ConcretePrototype concretePrototype = new ConcretePrototype (); concreteePrototype.setAttr(\"Tom\"); ConcretePrototype copy= (ConcretePrototype )concretePrototype.clone(); }}
public abstract class Prototype{ public abstract Prototype clone();}
public class ConcretePrototype extends Prototype{ private String attr; public String getAttr(){ return attr;} public void setAttr(String attr) { this.attr =attr;} public Prototype clone(){ ConcreteePrototype concreteePrototype = new ConcretePrototype(); concretePrototype.setAttr(this.attr); return concretePrototype; }}
ConcretePrototypeB
+clone():Prototype
ConcretePrototypeA(具体原型类)
Prototype(抽象原型类)
简化对象的创建过程扩展性较好简化的创建结构可以使用深克隆的方式保存对象的状态
违背了开闭原则在实现深克隆时需要编写较为复杂的代码
Client
+prototype:Prototype-copy:Prototype
+action()
0 条评论
下一页