定義(From 百度百科)
以原型實例指定建立物件的種類,並且透過拷貝這些原型建立新的物件。
UML類別圖表:
#
public class Client {public static void main(String[] args) {// Director d = new Director(new ConcreteBuilder());// d.construct();ConcretePrototype1 prototype = new ConcretePrototype1();for (int i = 0; i < 10; i++) { ConcretePrototype1 x = (ConcretePrototype1) prototype.clone(); x.test1(); } } }public class Prototype implements Cloneable {public Prototype clone() { Prototype prototype = null;try{ prototype = (Prototype)super.clone(); }catch(CloneNotSupportedException e){ e.printStackTrace(); }return prototype; } }public class ConcretePrototype1 extends Prototype {public void test1() { System.out.println(this); System.out.println("123"); } }
以上是設計模式之原型模式實例教程的詳細內容。更多資訊請關注PHP中文網其他相關文章!