[Design Mode] - prototype model Prototype

  Preface: [ Mode Overview ] ---------- by xingoo

  Intention mode

  Because sometimes, you need to specify which instance of the class when an object at run time, this time with the factory mode bit powerless. You can clone a function by copying the original object to the current object by using the prototype model to create more of the same type of object.

  Mode structure

  [ Simple prototype model when] a small prototype version

  [ Prototype model registration mode ] If the prototype implementation of a variety of versions, then register through a management class, can facilitate the realization of a prototype management.

  Prototype interfaces define the structure of the prototype Prototype.

  ConcretePrototype concrete realization of the prototype.

  Client use the class, create a prototype, create a reference, you can specify a class to be referenced at will.

  PrototypeManager prototype manager, which contains a the Map, for instance of an object stored prototype.

  scenes to be used

  When it is desired to achieve a specified object when the class at runtime.

  2 when a class instance can have only a centralized state. (This is not how to understand)

  Code structure

  [ Simple prototype model ]

 1 package com.xingoo.test;
 2 
 3 interface Prototype{
 4     public Object clone();
 5 }
 6 class ConcretePrototype1 implements Prototype{
 7     public Object clone() {
 8         Prototype prototype = new ConcretePrototype1();
 9         return prototype;
10     }
11 }
12 class ConcretePrototype2 implements Prototype{
13     public Object clone(){
14         Prototype prototype = new ConcretePrototype2();
15         return prototype;
16     }
17 }
18 public class Client{
19     public static void main(String[] args){
20         Prototype p1 = new ConcretePrototype1();
21         System.out.println("p1 "+p1);
22         
23         Prototype p2 = new ConcretePrototype2();
24         System.out.println("p2 "+p2);
25         
26         Prototype prototype = (Prototype)p1.clone();
27         System.out.println("prototype "+prototype);
28         prototype = (Prototype)p2.clone();
29         System.out.println("prototype "+prototype);
30     }
31 }

  operation result

p1 com.xingoo.test.ConcretePrototype1@1fb8ee3
p2 com.xingoo.test.ConcretePrototype2@14318bb
prototype com.xingoo.test.ConcretePrototype1@ca0b6
prototype com.xingoo.test.ConcretePrototype2@10b30a7

  [ Prototype pattern registration mode ]

  1 package com.xingoo.test1;
  2 
  3 import java.util.HashMap;
  4 import java.util.Map;
  5 /**
  6  * 原型的接口
  7  * @author xingoo
  8  */
  9 interface Prototype{
 10     public Prototype clone();
 11 }
 12 /**
 13  * 具体的实现类1
 14  * @author xingoo
 15  *
 16  */
 17 class ConcretePrototype1 implements Prototype{
 18     public Prototype clone() {
 19         Prototype prototype = new ConcretePrototype1();
 20         return prototype;
 21     }
 22 }
 23 /**
 24  * 具体的实现类2
 25  * @author xingoo
 26  *
 27  */
 28 class ConcretePrototype2 implements Prototype{
 29     public Prototype clone(){
 30         Prototype prototype = new ConcretePrototype2();
 31         return prototype;
 32     }
 33 }
 34 /**
 35  * 原型的管理器
 36  * @author xingoo
 37  *
 38  */
 39 class PrototypeManager{
 40     /**
 41      * 用于保存原型的实例
 42      */
 43     private static Map<String,Prototype> map = new HashMap<String,Prototype>();
 44     /**
 45      * 静态方法创建构造函数,避免外部类调用
 46      */
 47     private PrototypeManager(){
 48     }
 49     /**
 50      * 添加原型
 51      * @param protoName 原型的名字
 52      * @param prototype 原型的实例
 53      */
 54     public synchronized static void setPrototype(String protoName,Prototype prototype){
 55         map.put(protoName, prototype);
 56     }
 57     /**
 58      * 获得原型
 59      * @param protoName 原型的名字
 60      * @return 返回原型的实例
 61      * @throws Exception 如果找不到,则跑出找不到异常
 62      */
 63     public synchronized static Prototype getPrototype(String protoName) throws Exception{
 64         Prototype prototype = map.get(protoName);
 65         if(prototype == null){
 66             throw new Exception("no "+protoName+" in Manager");
 67         }
 68         return prototype;
 69     }
 70     /**
 71      * 从管理器中删除原型的实例
 72      * @param protoName 原型的名字
 73      */
 74     public synchronized static void removedPrototype(String protoName){
 75         map.remove(protoName);
 76     }
 77 }
 78 /**
 79  * 原型的使用者
 80  * @author xingoo
 81  *
 82  */
 83 public class Client {
 84     public static void main(String[] args){
 85         try{
 86             /**
 87              * 创建一种原型的实现,放入管理器中
 88              */
 89             Prototype p1 = new ConcretePrototype1();
 90             System.out.println("p1 "+p1);
 91             PrototypeManager.setPrototype("MyPrototype", p1);
 92             
 93             Prototype prototype1 = PrototypeManager.getPrototype("MyPrototype").clone();
 94             System.out.println("prototype1 "+prototype1);
 95             /**
 96              * 切换成另一种原型的实现,修改管理器中的对象
 97              */
 98             Prototype p2 = new ConcretePrototype1();
 99             System.out.println("p2 "+p2);
100             PrototypeManager.setPrototype("p1", p2);
101             
102             Prototype prototype2 = PrototypeManager.getPrototype("MyPrototype").clone();
103             System.out.println("prototype2 "+prototype2);
104             /**
105              * 注销该原型实现,对象使用后,观察情况
106              */
107             PrototypeManager.removedPrototype("MyPrototype");
108             
109             Prototype prototype3 = PrototypeManager.getPrototype("MyPrototype").clone();
110             System.out.println("prototype3 "+prototype3);
111             
112         }catch(Exception e){
113             e.printStackTrace();
114         }
115     }
116 }

  operation result

p1 com.xingoo.test1.ConcretePrototype1@116ab4e
prototype1 com.xingoo.test1.ConcretePrototype1@129f3b5
p2 com.xingoo.test1.ConcretePrototype1@13f3045
prototype2 com.xingoo.test1.ConcretePrototype1@17a29a1
java.lang.Exception: no MyPrototype in Manager
    at com.xingoo.test1.PrototypeManager.getPrototype(Client.java:66)
    at com.xingoo.test1.Client.main(Client.java:109)

 

Reproduced in: https: //my.oschina.net/u/204616/blog/545256

Guess you like

Origin blog.csdn.net/weixin_33972649/article/details/91989277