用意:通過給出一個原型對象來指明所要創建的對象的類型,然後用複製這個原型對象的辦法創建出更多同類型的對象。
1. com.cleversoft.designpatterns.prototype.Prototype.java:
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> interface Prototype {
500)this.width=500" align=top border=0> Object clone();
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0> String getName();
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0> void execute();
500)this.width=500" align=top border=0>}
2. com.cleversoft.designpatterns.prototype.This.java:
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0>class This implements Prototype {
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public Object clone() {
500)this.width=500" align=top border=0> return new This();
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public String getName() {
500)this.width=500" align=top border=0> return "This";
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public void execute() {
500)this.width=500" align=top border=0> System.out.println("This: execute");
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>}
3. com.cleversoft.designpatterns.prototype.That.java:
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0>class That implements Prototype {
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public Object clone() {
500)this.width=500" align=top border=0> return new That();
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public String getName() {
500)this.width=500" align=top border=0> return "That";
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public void execute() {
500)this.width=500" align=top border=0> System.out.println("That: execute");
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>}
4. com.cleversoft.designpatterns.prototype.PrototypesModule.java:
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0>class PrototypesModule {
500)this.width=500" align=top border=0> private static Prototype[] prototypes = new Prototype[9];
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0> private static int total = 0;
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public static void addPrototype(Prototype obj) {
500)this.width=500" align=top border=0> prototypes[total++] = obj;
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public static Object findAndClone(String name) {
500)this.width=500" align=top border=0> for (int i = 0; i < total; i++)
500)this.width=500" align=top border=0> if (prototypes[i].getName().equals(name))
500)this.width=500" align=top border=0> return prototypes[i].clone();
500)this.width=500" align=top border=0> System.out.println(name + " not found");
500)this.width=500" align=top border=0> return null;
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>}
5. com.cleversoft.designpatterns.prototype.Main.java:
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0>public class Main {
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public static void initializePrototypes() {
500)this.width=500" align=top border=0> PrototypesModule.addPrototype(new This());
500)this.width=500" align=top border=0> PrototypesModule.addPrototype(new That());
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> public static void main(String[] args) {
500)this.width=500" align=top border=0> initializePrototypes();
500)this.width=500" align=top border=0> Object[] objects = new Object[9];
500)this.width=500" align=top border=0> int total = 0;
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0>500)this.width=500" align=top border=0> for (int i = 0; i < args.length; i++) {
500)this.width=500" align=top border=0> objects[total] = PrototypesModule.findAndClone(args[i]);
500)this.width=500" align=top border=0> if (objects[total] != null)
500)this.width=500" align=top border=0> total++;
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0> for (int i = 0; i < total; i++)
500)this.width=500" align=top border=0> ((Prototype) objects[i]).execute();
500)this.width=500" align=top border=0>
500)this.width=500" align=top border=0> }
500)this.width=500" align=top border=0>}
參考資料:
1. 《Java與模式》