(一)原型模式

2017-04-06  本文已影响0人  野狗子嗷嗷嗷

概念

用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。

优缺点

结构与参与者

代码示例

// 原型类要实现Cloneable接口,在运行时通知虚拟机可以安全地在实现了此接口的类上使用clone方法,因为只有实现了这个接口的类才可以被拷贝
class Prototype implements Cloneable {  
    public Prototype clone(){  
        Prototype prototype = null;  
        try{  
            prototype = (Prototype)super.clone();  
        }catch(CloneNotSupportedException e){  
            e.printStackTrace();  
        }  
        return prototype;   
    }  
}  
  
class ConcretePrototype extends Prototype{  
    public void show(){  
        System.out.println("原型模式实现类");  
    }  
}  
  
public class Client {  
    public static void main(String[] args){  
        ConcretePrototype cp = new ConcretePrototype();  
        for(int i=0; i< 10; i++){  
            ConcretePrototype clonecp = (ConcretePrototype)cp.clone();  
            clonecp.show();  
        }  
    }  
}  

参考资料

设计模式专栏

Youtube
Android开发中无处不在的设计模式——原型模式
23种设计模式(5):原型模式
Prototype Pattern
Prototype Pattern Tutorial with Java Examples
Design pattern: singleton, prototype and builder
原型模式(Proxy Pattern)
What's the point of the Prototype design pattern?
设计模式包教不包会
六个创建模式之原型模式(Prototype Pattern)

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

上一篇下一篇

猜你喜欢

热点阅读