Design Mode (21) --- visitor mode, design mode visitor

Source: Internet
Author: User

Design Mode (21) --- visitor mode, design mode visitor

I. Definition

Visitor mode: indicates an operation that acts on each element in an object structure. It allows you to define the role and new operations of each element without changing the class of each element.

Explanation: if the elements of an object are fixed, you can use the visitor mode to define their operations so that the operations can be freely increased or reduced without affecting other parts of the system.

 

Ii. UML class diagram and basic code

Basic code:

Abstract class Visitor {public abstract void principal (ConcreteElementA concreteElementA); public abstract void principal (ConcreteElementB principal);} class principal: Visitor {public override void principal (principal ConcreteElementA) {Console. writeLine ("{0} accessed by {1}", concreteElementA. getType (). name, this. getType (). name);} public override void VisitConcreteElementB (ConcreteElementB concreteElementB) {Console. writeLine ("{0} accessed by {1}", concreteElementB. getType (). name, this. getType (). name) ;}} class ConcreteVisitor2: Visitor {public override void VisitConcreteElementA (ConcreteElementA concreteElementA) {Console. writeLine ("{0} accessed by {1}", concreteElementA. getType (). name, this. getType (). name);} public override void VisitConcreteElementB (ConcreteElementB concreteElementB) {Console. writeLine ("{0} accessed by {1}", concreteElementB. getType (). name, this. getType (). name) ;}} abstract class Element {public abstract void Accept (Visitor visitor);} class ConcreteElementA: Element {public override void Accept (Visitor visitor) {visitor. visitConcreteElementA (this);} public void OperationA () {}} class ConcreteElementB: Element {public override void Accept (Visitor visitor) {visitor. visitConcreteElementB (this);} public void OperationB () {}} class ObjectStructure {private IList <Element> elements = new List <Element> (); public void Attach (Element element) {elements. add (element);} public void Detach (Element element) {elements. remove (element);} public void Accept (Visitor visitor) {foreach (Element e in elements) {e. accept (visitor );}}}

 

Client call and permitted results:

ObjectStructure OS = new ObjectStructure (); OS. attach (new ConcreteElementA (); OS. attach (new ConcreteElementB (); ConcreteVisitor1 v1 = new ConcreteVisitor1 (); ConcreteVisitor2 v2 = new ConcreteVisitor2 (); OS. accept (v1); OS. accept (v2 );View Code

 

Iii. instance description

Introduce the differences between men and women. Currently, the elements include that men and women are fixed, but their differences are that there are not many operations. For example, when a man succeeds, most of them are great women. When a woman succeeds, there is usually a man behind the scenes who fails. When a man fails, he gets bored and drinks, and no one needs to persuade him or her. When a woman fails, he or she will not be able to persuade anyone. Code Description:

 

First, write abstract humans and specific objects for men and women:

abstract class Person    {        public abstract void Accept(Action action);    }    class Man : Person    {        public override void Accept(Action action)        {            action.GetManConclusion(this);        }    }    class Woman : Person    {        public override void Accept(Action action)        {            action.GetWomanConclusion(this);        }    }

 

Then write the abstract operation class and specific operation behavior of the element:

Abstract class Action {public abstract void GetManConclusion (Man clusion); public abstract void GetWomanConclusion (Woman concreteElementB);} class Success: Action {public override void GetManConclusion (Man concreteElementA) {Console. when WriteLine ("{0} {1}, there is usually a great woman behind it", concreteElementA. getType (). name, this. getType (). name);} public override void GetWomanConclusion (Woman concreteElementB) {Console. when WriteLine ("{0} {1}, there is mostly an unsuccessful man behind it", concreteElementB. getType (). name, this. getType (). name) ;}} class Fail: Action {public override void GetManConclusion (Man concreteElementA) {Console. when WriteLine ("{0} {1}, no one needs to persuade anyone to drink.", concreteElementA. getType (). name, this. getType (). name);} public override void GetWomanConclusion (Woman concreteElementB) {Console. when WriteLine ("{0} {1}, no one can persuade me.", concreteElementB. getType (). name, this. getType (). name );}}

 

Finally, write a structure class, which enumerates elements and provides a high-level interface to allow visitors to access its elements.

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 action)        {            foreach (Person e in elements)            {                e.Accept(action);            }        }    }

 

Client call and result:

ObjectStructure os = new ObjectStructure();            os.Attach(new Man());            os.Attach(new Woman());            Success s = new Success();            os.Display(s);            Fail f = new Fail();            os.Display(f);

 

Iv. Advantages and Disadvantages and applicable scenarios

Advantages:

The visitor mode makes it easy to add new operations. If some operations depend on a complex structure object, it is generally complicated to add new operations. In the visitor mode, adding a new operation means adding a new visitor class. Therefore, it is easy to add new operations.

Disadvantages:

It is difficult to add new element classes. Adding a new element means adding a new abstract operation to the abstract visitor role and adding the corresponding specific operation to each specific visitor class.

 

Applicable scenarios:

To put it bluntly, it can be used to show its advantages and avoid its disadvantages.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.