Visitor mode

Motivation

In the software construction process, due to changes in requirements, new behaviors (methods) are often added to certain class hierarchies. If such changes are made directly in the base class, it will bring a heavy burden of changes to the subclasses , And even destroy the original design.

How to dynamically add new operations for each class in the class hierarchy as needed at runtime without changing the class hierarchy, so as to avoid the above problems?

Intent

Represents an operation that acts on each element in an object structure. It can define new operations that act on these elements without changing the class of each element.

Structure (Structure)

Visitor class: declare a visit operation for each class of ConcreteElement in the object structure

ConcreteVisitor specific visitor class: implement each operation declared by the visitor, each operation implements a part of the algorithm, and the algorithm fragment is still the class corresponding to the object in the structure

ObjectStructure object structure class: can enumerate its elements, and can provide a high-level interface to allow visitors to access its elements

Element class: define an accept operation, which takes a visitor as a parameter

ConcereteElement class: specific element class, to achieve Accept operation

Several key points of Visitor mode

1. The visitor mode uses the so-called double dispatch to transparently add new operations to each dynamic in the class hierarchy at runtime without changing the Element class hierarchy.

2. The so-called double distribution means that the visitor mode includes two polymorphic branches. Pay attention to the polymorphism mechanism. The first is the polymorphism analysis of the accept method; the second is the polymorphism analysis of the visit method

3. The biggest disadvantage of the visitor mode is that the extension of the class hierarchy (adding a new Element class) will cause the visitor class to change. Therefore, the Visitor mode is suitable for "Element class hierarchical structure is stable", and its operations often face frequent changes.

Code

The human abstract class, corresponding to the Element class in the structure diagram, defines an accept method (operation), which takes the visitor as a parameter

    abstract class Person
    {
        //接受方法,用来获得状态对象的
        public abstract void Accept(Action visitor);
    }

The Man class and the Woman class are concrete element classes, corresponding to the ConcreteElement class in the structure diagram, which implements the Accept operation defined by the Element class, which is human

    //男人类
    class Man : Person
    {
        public override void Accept(Action visitor)
        {
            visitor.GetManConclusion(this);
        }
    }
    //女人类
    class Woman : Person
    {
        public override void Accept(Action visitor)
        {
            visitor.GetWomanConclusion(this);
        }
    }

The Action state abstract class, which corresponds to the Visitor class in the structure diagram, declares a visit operation for each class of ConcreteElement in the structure. In this example, declares a GetManConclusion operation and a GetWomanConclusion operation for the man and woman classes.

    abstract class Action
    {
        //得到男人结论或反应
        public abstract void GetManConclusion(Man concreteElementA);
        //得到女人结论或反应
        public abstract void GetWomanConclusion(Woman concreteElemanetB);
    }

Specific status classes: success class, failure class, love class, corresponding to ConcreteVisitor in the structure diagram, to achieve the two operations declared by Visitor

    class Success : Action
    {
        public override void GetManConclusion(Man concreteElementA)
        {
            Console.WriteLine("{0}{1}时,背后多半有一个伟大的女人。",concreteElementA.GetType().Name,this.GetType().Name);
        }
        public override void GetWomanConclusion(Woman concreteElementB)
        {
            Console.WriteLine("{0}{1}时,背后大多有一个不成功的男人。",concreteElementB.GetType().Name,this.GetType().Name);
        }
    }

Object structure class: corresponding to ObjectStructure in the structure diagram

    class ObjectStructure
    {
        private IList<Person> elements = new List<Person>();
        //增加
        public void Attach(Person element)
        {
            elements.Add(element);
        }
        //移除
        public void Detach(Person element)
        {
            elements.Remove(element);
        }
        //查看显示
        public void Display(Action visitor)
        {
            foreach (Person e in elements)
            {
                e.Accept(visitor);
            }
        }
    }

Client code

Men and women to be contrasted in the object structure

 ObjectStructure o = new ObjectStructure();
 o.Attach(new Man());
 o.Attach(new Woman());

The reaction to success, the Display() method is used to view the reaction of men and women in various states

Success v1 = new Success();
o.Display(v1);

Reaction on failure

Failing v2 = new Failing();
o.Display(v2);

Summary: The visitor pattern is suitable for systems with relatively stable data structures and algorithms that are easy to change. This is suitable for the visitor pattern.

Guess you like

Origin blog.csdn.net/wtt15100/article/details/105928284