Step by step study notes twenty-one, Visitor (Visitor pattern) for the camp .NET design patterns

Outline

The element operation represents a certain object structure. It makes you can not change the definition of the role of each element under the premise of the new class of operation of these elements, that the coupling between the structure and the operation of acting on the data structure to be freed, so that the combined operation can be relatively freely evolve . The advantage is adding a new operation is very easy, because adding a new operation would mean adding a new visitor, the visitor pattern of conduct relevant to a centralized access object.

Intention
to achieve operating through a unified interface to access different types of elements, and through this interface can add a new operation without changing the classes of elements.

Structure chart

image

Role Description:

The role of visitors (Visitor): a user interface for accessing specific elements of the role of object structure declaration. The name and operational parameters of the interface identifies specific elements of role access request is sent to specific visitors. So that visitors can pass the role of specific interface elements direct access to it.

Visitors specific role (Concrete Visitor): implementation of each operation by the role of visitors (Visitor) statement.

The role of elements (Element): Accept a defined action, which to a visitor as an argument.

Specific elements of the role (Concrete Element): Accept implement operations provided by the role of elements.

Object of roles (Object Structure): This is the essential role of the use of visitors. It should have the following characteristics: be able to enumerate its elements; can provide a high-level interface to allow the visitor to access its elements; may be a composite (combination pattern) or a collection, such as a list or a set of unordered .

 

Life examples

Such as a park, to have a plurality of different components; a plurality of the park visitor: A cleaning A is responsible for cleaning of the part of the park, is responsible for cleaning cleaners B Part B of the park, the park manager responsible for the discreet whether the transaction is complete, the higher-ups may also visit Park.

 

FIG exemplary embodiment using

Boys and girls have a love in life when a different shape, for want of a problem and is not the same way, we have designed a visitor pattern, use case diagram as follows:

image

 

Code design

Create Person.cs:

    public interface Person
    {
         string Accept(Visitor visitor); 
    }

 

Then create Boy.cs:

    public class Boy : Person
    {
        #region Person 成员

        public string Accept(Visitor visitor)
        {
            return visitor.Visit(this);
        }

        #endregion
    }

 

Then create Gril.cs:

    public class Gril : Person
    {
        #region Person 成员

        public string Accept(Visitor visitor)
        {
            return visitor.Visit(this);
        }

        #endregion
    }

 

Then create Visitor.cs:

    public interface Visitor
    {
        string Visit(Boy boy);

        string Visit(Gril gril);
    }

 

Then create Action.cs:

    public class Action : Visitor
    {
        #region Visitor 成员

        public string Visit(Boy boy)
        {
            return "男孩遇到事情表现的勇敢与智慧的样子.";
        }

        public string Visit(Gril gril)
        {
            return "女孩遇到事情表现的可爱与优雅的样子.";
        }

        #endregion
    }

 

Then create Think.cs:

    public class Think : Visitor
    {
        #region Visitor 成员

        public string Visit(Boy boy)
        {
            return "男孩喜欢胡思乱想";
        }

        public string Visit(Gril gril)
        {
            return "女孩喜欢天真浪漫";
        }

        #endregion
    }

 

再创建PersonStructure.cs:

    public class PersonStructure
    {
        public List<Person> PersonList = new List<Person>();

        public void Add(Person person)
        {
            PersonList.Add(person);
        }

        public void Remove(Person person)
        {
            PersonList.Remove(person);
        }

        public string Action(Visitor visitor)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Person person in PersonList)
            {
                sb.AppendLine(person.Accept(visitor));
            }
            return sb.ToString();
        }
    }

 

最后再调用:

 

    public partial class Run : Form
    {
        public Run()
        {
            InitializeComponent();
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            //-------------------------------------

            PersonStructure personStructure = new PersonStructure();
            personStructure.Add(new Boy());
            personStructure.Add(new Gril());
            rtbResult.AppendText(personStructure.Action(new Visitor.Action()));
            rtbResult.AppendText(personStructure.Action(new Think()));
        }

    }

 

结果如下图:

image

 

实现要点

1.每个元素都需要设置Accept()方法来接受访问者。

2.两次多态分发,确定访问者以及访问者中的方法。

3.如果一个结构层次中有多个类型的元素,那么可以通过一个ObjectStructure的角色进行封装。

 

适用性

1.对象结构中包含很多类型,这些类型没有统一的接口,而我们又希望使得对象的操作进行统一。

2.希望为对象结构中的类型新增操作,并且不希望改变原有的代码。

3.对象结构中的一些类型之间发生耦合,而它们的实现又经常会发生变动。

4.针对结构中同一层次的不同类型甚至是不同层次的类型进行迭代。

5.需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而你想避免让这些操作“污染”这些对象的类。Visitor使得你可以将相关的操作集中起来定义在一个类中。

6.当该对象结构被很多应用共享时,用Visitor模式让每个应用仅包含需要用到的操作。

7.定义对象结构的类很少改变,但经常需要在此结构上定义新的操作。改变对象结构类需要重定义对所有访问者的接口,这可能需要很大的代价。如果对象结构类经常改变,那么可能还是在这些类中定义这些操作较好。

 

优点

1.分离对象的数据结构与行为,让不同的类完成不同的功能
2.可以不修改已有类的基础上增加新的操作行为
3.从另一个角度来看,同一个数据结构,为其实现不同的观察者,便可呈现不同的行为。

 

缺点

1.难以扩展对象结构,其实,这点是可以通过一些变化进行化解的。

2.需要过多暴露对象的内部元素,否则访问者难以对对象进行实质性的操作。

3.需要实现考虑到这样的需求并且提前设置接受访问者的方法。

 

总结

1.这是一个巧妙而且复杂的模式,它的使用条件比较苛刻。当系统中存在着固定的数据结构(比如上面的类层次),而有着不同的行为,那么访问者模式也许是个不错的选择。

Reproduced in: https: //www.cnblogs.com/springyangwc/archive/2011/05/02/2034716.html

Guess you like

Origin blog.csdn.net/weixin_33795743/article/details/93340894