Categories
Uncategorized

Design Mode – Behavioral – Visitor Pattern

Visitor pattern (Visitor):

The official definition of the visitor pattern is this: An operation, each element of an object structure effect, it makes you can not change the definition of the elements under the premise of the new class role in the operation of these elements. Official things are obscure, we can now look to the dismantling: First of all “the elements of a role of an object structure of the operation,” referring to three things: the object structure, elements, operations. We have studied data structures, data structures you access to the data are generally direct access to its address. In object-oriented design, we are generally the data access operation on the inner classes, for easy access. This design seems to be no problem, but when we want to use different ways it is necessary to modify the class to access the data structure or object, that would violate the principle of the OCP. So we think of the data structure and operations separated, when asked who need to add direct access operations when adding a new class, the original code does not need to make any changes, which is also mentioned in the last part, “can not be changed definition of the role of each element under the premise of the new class of operation of these elements. “

Visitor pattern of roles:

  

1) Visitor: interface or abstract class that defines its behavior parameters for each element is accessed Element is accessed, its number of elements and the number of theoretical method is the same, therefore, the visitor mode requires the type of element to stabilize, if often added, removed element class, will inevitably lead to frequent modifications visitor interfaces, if this is the case, then the mode is not suitable.

2) ConcreteVisitor: specific visitors, it needs to give a specific element against every type of access a generated.

3) Element: element interface or abstract class that defines methods (accept) receiving a visitor, meaning that every element which can be accessed visitors.

4) ConcreteElement: concrete elements class that provides a specific implementation of the interview, but this method is usually embodied element class with access to the visitor provided.

5) ObjectStructure: defines which objects mentioned structure, the structure of the object is an abstract representation, the set of elements that internal management, and may iterate elements provide visitors.

Example:

There is a directory of folders and files, folders and files are specific elements of the class.

 1 /// 
 2 /// 抽象访问者
 3 /// 
 4 public abstract class Visitor
 5 {
 6     public abstract void visit(File file);
 7 
 8     public abstract void visit(Directory directory);
 9 }
10 
11 /// 
12 /// 具体的访问者
13 /// 
14 public class ListVisitor : Visitor
15 {
16     public override void visit(File file)
17     {
18         Console.WriteLine($"

File name {file.GetName ()}

"); 19 } 20 21 public override void visit(Directory directory) 22 { 23 Console.WriteLine($"

Folder name {directory.GetName ()}

"); 24 } 25 } 26 27 /// 28 /// 元素抽象类 29 /// 30 public abstract class Element 31 { 32 public abstract void accept(Visitor visitor); 33 } 34 35 public class Entry : Element 36 { 37 private string name; 38 39 public Entry(string name) 40 { 41 this.name = name; 42 } 43 44 public string GetName() 45 { 46 return this.name; 47 } 48 49 public override void accept(Visitor visitor) 50 { 51 throw new NotImplementedException(); 52 } 53 } 54 55 /// 56 /// 具体的元素类:文件夹 57 /// 58 public class Directory : Entry 59 { 60 public Directory(string name) 61 : base(name) 62 { 63 } 64 65 public override void accept(Visitor visitor) 66 { 67 visitor.visit(this); 68 } 69 } 70 71 /// 72 /// 具体的元素类:文件 73 /// 74 public class File : Entry 75 { 76 public File(string name) 77 : base(name) 78 { 79 } 80 81 public override void accept(Visitor visitor) 82 { 83 visitor.visit(this); 84 } 85 }

Defined, data manipulation, the next core class ObjectStructure we define data structures.

 1 public class ObjectStructure
 2 {
 3     private readonly Visitor visitor;
 4     private List list = new List();
 5 
 6     public ObjectStructure(Visitor visitor)
 7     {
 8         this.visitor = visitor;
 9     }
10 
11     public void Add(Element element)
12     {
13         list.Add(element);
14     }
15 
16     public void Show()
17     {
18         foreach (Element element in list)
19         {
20             element.accept(visitor);
21         }
22     }
23 }

Client calls.

 1 private static void Main(string[] args)
 2 {
 3     ObjectStructure objectStructure = new ObjectStructure(new ListVisitor());
 4     objectStructure.Add(new Directory("

my file

")); 5 objectStructure.Add(new File("

C # Advanced Programming .pdf

")); 6 objectStructure.Add(new File("

Design Patterns .pdf

")); 7 objectStructure.Add(new File("

Algorithm .txt

")); 8 objectStructure.Add(new File("mm.png")); 9 objectStructure.Show(); 10 }

Visitor pattern of strengths and weaknesses:

advantage:

1) the role of each separation of duties, in line with the principle of single responsibility.

2) has excellent extensibility, such that the data structure and the operation of acting on the decoupling structure, the set of operations that can be varied independently.

3) Flexibility

Disadvantages:

1) specific elements of the visitor announced details of a violation of the principle of Demeter.

2) specific elements of change more difficult.

3) violates the Dependency Inversion principle, we rely on a specific class that does not rely on abstraction.

Visitors using the scene modes:

1) the object corresponding to the object class structure rarely changes, but often need to define a new operation on the object structure.

2) the need for a lot of different and unrelated to the operation of a target object structure, and these actions need to avoid “contamination” class of these objects, we do not want to modify these classes when adding new operations.

Reference: https: //www.cnblogs.com/zyrblog/p/9244754.html

Leave a Reply