Head First Design Patterns - the prototype model and Visitor Pattern

prototype

Prototype mode: When you create a process given class is very expensive or very complex, on the use of the prototype model.

We are playing the game when the game will dynamically create strange, but when strange varies according to different scenarios created by the hero himself will create some followers. Create a wide variety of monsters instance, has become increasingly troublesome, will put the details of the various states and then the constructor, it did not look cohesive. If you can encapsulate all the details of instances within a single area, it is possible to create a code that handles the details of monsters, decoupled from the code actually need to dynamically create an instance, the program will become fairly clean.

Prototype pattern to create a new instance by copying an existing instance, the clone method is generally used, or deserialization.

Design class diagram:

Prototype model has three roles:

Prototype roles : duplicating an existing instance definition method generates a new instance (Monster).

Specific roles Prototype : implementation (WellKnowMonster, DynamicGeneratedMonster) for generating a new instance of copying an existing instance.

User roles : maintaining a registry, and provide a method to find the correct instance of the prototype. Finally, there is provided a method of obtaining new instance, the method used to delegate new instances of the replicated instance.

Implementation code:

① prototype role

1     public interface Monster
2     {
3         public Monster Clone();
4     }

② specific role prototype

 1     public class WellKnowMonster : Monster
 2     {
 3         public Monster Clone()
 4         {
 5             Monster clone = JsonConvert.DeserializeObject<WellKnowMonster>(JsonConvert.SerializeObject(this));
 6             return clone;
 7         }
 8     }
 9 
10 
11     public class DynamicGeneratedMonster : Monster
12     {
13         public Monster Clone()
14         {
15             Monster clone = JsonConvert.DeserializeObject<DynamicGeneratedMonster>(JsonConvert.SerializeObject(this));
16             return clone;
17         }
18     }

③ user roles

 1     public class MonsterRegistry
 2     {
 3         Dictionary<string, Monster> monsterDic = new Dictionary<string, Monster>();
 4         public void RegisterMonster(string key,Monster monster) {
 5             monsterDic.Add(key, monster);
 6         }
 7         public Monster GetMonster(string key) {
 8             Monster monster = monsterDic[key];
 9              return monster.Clone();
10         }
11     }

④ test

 advantage:

1, hide the complexity of creating new instances of the customer.

2, provide customer can produce objects of unknown type option.

3, in certain circumstances, copy the object is more efficient than creating a new object.

Uses and Disadvantages:

1, in a complex class hierarchy, when the system must create a new object from many types, consider the prototype.

2, the disadvantage of using the prototype model, copying objects sometimes quite complex.

Visitors

Visitors mode: When you want to add new capabilities as a combination of an object, and the package is not important, it is the use of visitors.

When inside the restaurant to the customer, the customer may be asked information menu (such as the red pepper, the taste is not heavy like), and even some will ask composition of raw materials.

We like this design to add a new method in each place, if adding a new method we have to add a new method in two places, in case more than a new menu we have to modify three places. In this case we can use the visitor pattern. By each element in the visitors combinations, the state of all objects in the collection. Once the state is collected, customers can allow visitors to the state of various operations. When you need new functionality, as long as the strengthening of the visitor can be.

 Implementation code:

① achieve menus, menu items GetState interface definition method

. 1      public  interface of MenuComponent
 2      {
 . 3          public  void GetState (Visitor hit);
 . 4      }
 . 5  
. 6  
. 7      // menu 
. 8      public  class Menu: of MenuComponent
 . 9      {
 10          public  String displayInfo = " not hot, moderate taste " ;
 . 11          public  void GetState (Visitor hit)
 12 is          {
 13 is              visitor.Visit ( the this );
 14          }
 15     }
16 
17     //原料
18     public class Ingredients : MenuComponent
19     {
20         public string displayInfo = "不辣,偏咸";
21         public void GetState(Visitor visitor)
22         {
23             visitor.Visit(this);
24         }
25     }

② visitors, visitors to define the interface using Interface Type menu item

    public interface Visitor
    {
        public void Visit(Menu menu);
        public void Visit(Ingredients ingredients);
    }


    class MenuVisitor : Visitor
    {
        public void Visit(Menu menu)
        {
            Console.WriteLine(menu.displayInfo);
        }

        public void Visit(Ingredients ingredients)
        {
            Console.WriteLine(ingredients.displayInfo);
        }
    }

③ test

advantage:

1, allows you to join a new operating structure for the combination, without changing the structure itself.

2, want to join the new operation is relatively easy.

3, the operation performed by the visitor, which code is together.

Uses and Disadvantages:

1, when using the visitor pattern, the package will break class combination.

2, because the addition of access patterns as required for each visit, so changes to the portfolio structure more difficult.

Guess you like

Origin www.cnblogs.com/SunSpring/p/12507777.html