Instance of a pattern--visitor instance

Source: Internet
Author: User

Scene: Coach ticket, train ticket price adjustment

Description: The Spring festival began, the peak of people, working crowd to go home for the new year, the price will rise!

1. Abstract visitor (Visitor, Program: notifyvisitor) Role: declares one or more access operations that form the interface that all concrete element roles must implement.

2, the specific visitor (Concretevisitor, the program for the various periods of fare fluctuations) role: the implementation of the abstract visitor role declared interface, that is, the abstract visitor declared the individual access operations.

3, abstract node (Element, the vehicle base class in the program) role: Declare an accept operation, accept a visitor object as a parameter.

4, the specific node (concreteelement, the various types of transport in the program) role: the implementation of the abstract elements of the accepted operation.

5. Structure object (obiectstructure, traffic management department in the program) role: There are some responsibilities that can traverse all elements of the structure, and if required, provide a high-level interface for the visitor object to access each element; Can be designed as a composite object or as a cluster, such as a column (list) or collection (set).

In the process of software construction, due to the change of requirements, some class hierarchies often need to add new behavior (methods), if the direct in the base class to make such a change, it will bring a heavy burden on the sub-class change, or even destroy the original design.
How do you avoid this problem by dynamically adding new operations to classes on class hierarchies at run time, without changing the class hierarchy, as needed transparently?

Identifies an action 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.

Implement 1 : When you don't use a visitor

(i) Transport standards

Transport

Public abstract class Vehicle

{

public abstract void Showprice ();

Chinese New Year price

public abstract void Pricefloatup ();

}

(b) Coach and train

Bus

public class Bus:vehicle

{

public override void Showprice ()

{

Console.WriteLine ("Coach Shijiazhuang to Xingtai 50 Yuan");

}

public override void Pricefloatup ()

{

Console.WriteLine ("Spring Festival began, car tickets on the basis of the original price, up 20%!" ");

}

}

Train

public class Train:vehicle

{

public override void Showprice ()

{

Console.WriteLine ("Train Shijiazhuang to Xingtai 40 Yuan");

}

public override void Pricefloatup ()

{

Console.WriteLine ("Spring Festival has begun, train tickets on the basis of the original price, up 15%!" ");

}

}

(iii) Testing

Bus bus = new bus ();

Bus. Showprice ();

Bus. Pricefloatup ();

Train Train = new Train ();

Train. Showprice ();

Train. Pricefloatup ();

Results:

Bus Shijiazhuang to Xingtai -Yuan

Spring Festival began, car tickets on the basis of the original price, floating 20%!

Train Shijiazhuang to Xingtai -¥

Spring Festival began, train tickets on the basis of the original price, floating 15%!

(iv) Special days, price increases

During the national day, fares will also rise!

Now you have to add a new price increase notice on long haul and train. Then add the interface in the base class:

National Day Price increase

public abstract void Pricenationdayfloatup ();

Add the implementation in both implementations:

public override void Pricenationdayfloatup ()

{

Console.WriteLine ("National Day, car ticket on the basis of the original price, floating 5%!") ");

}

......

public override void Pricenationdayfloatup ()

{

Console.WriteLine ("National Day, train tickets on the basis of the original price, floating 3%!") ");

}

(v) Testing

Bus bus = new bus ();

Bus. Showprice ();

Bus. Pricenationdayfloatup ();

Train Train = new Train ();

Train. Showprice ();

Train. Pricenationdayfloatup ();

Results:

Bus Shijiazhuang to Xingtai -Yuan

During the national day, car tickets on the basis of the original price, floating 5%!

Train Shijiazhuang to Xingtai -¥

During the National day, train tickets on the basis of the original price, floating 3%!

If the country's productivity in the future is highly developed and fares are lowered, then the respective notification methods should be added based on the two class implementations.

The entire notification system is now implemented as a visitor.

(a) Visitor abstraction

Prices always change, in different times, always have to increase prices. Therefore the price factor is here for the visitor.

Above the notice, the price increase

Public abstract class Notifyvisitor

{

public abstract void Visit (bus bus);

public abstract void Visit (Train Train);

}

(ii) Passenger transport sector

The coach, the train department receives the visitor's notice.

Transport

Public abstract class Vehicle

{

public abstract void Showprice ();

Chinese New Year price

public abstract void Accept (Notifyvisitor visitor);

}

(iii) Realization of the passenger transport sector

Bus

public class Bus:vehicle

{

public override void Showprice ()

{

Console.WriteLine ("Coach Shijiazhuang to Xingtai 50 Yuan");

}

public override void Accept (Notifyvisitor visitor)

{

Visitor. Visit (this);

}

}

Train

public class Train:vehicle

{

public override void Showprice ()

{

Console.WriteLine ("Train Shijiazhuang to Xingtai 40 Yuan");

}

public override void Accept (Notifyvisitor visitor)

{

Visitor. Visit (this);

}

}

(iv) The price of Spring festival

This is an implementation of the visitor

public class Newyearvisitor:notifyvisitor

{

public override void Visit (Bus bus)

{

Bus. Showprice ();

Console.WriteLine ("Spring Festival began, car tickets on the basis of the original price, up 20%!" ");

}

public override void Visit (Train Train)

{

Train. Showprice ();

Console.WriteLine ("Spring Festival has begun, train tickets on the basis of the original price, up 15%!" ");

}

}

Its purpose is to inform the two departments, to increase prices and the details of the price increase.

(v) Traffic Management Department

Used to determine which departments to raise prices. This is assignable: There are many kinds of vehicles, long-distance bus and train are two of them, this time is both to rise!

public class Traffimnagement

{

ilist<vehicle> _list = new list<vehicle> ();

public void Add (Vehicle Vehicle)

{

_list. ADD (vehicle);

}

public void Detach (Vehicle Vehicle)

{

_list. Remove (vehicle);

}

public void Accept (Notifyvisitor visitor)

{

foreach (Vehicle vv in _list)

{

vv. Accept (visitor);

}

}

}

(vi) Testing

public void Testvisitor ()

{

Traffimnagement Department = new Traffimnagement ();

Department. ADD (New Bus ());

Department. ADD (New Train ());

Department. Accept (New Newyearvisitor ());

}

Results:

Bus Shijiazhuang to Xingtai -Yuan

Spring Festival began, car tickets on the basis of the original price, floating 20%!

Train Shijiazhuang to Xingtai -¥

Spring Festival began, train tickets on the basis of the original price, floating 15%!

(vii) Rising prices during National day

New Canada National Day visitors, others do not change.

public class Nationaldaynotifyvisitor:notifyvisitor

{

public override void Visit (Bus bus)

{

Bus. Showprice ();

Console.WriteLine ("National Day, car ticket on the basis of the original price, floating 5%!") ");

}

public override void Visit (Train Train)

{

Train. Showprice ();

Console.WriteLine ("National Day, the train ticket on the basis of the original price, floating 3%!") ");

}

}

(eight) test

public void Testvisitor ()

{

Traffimnagement Department = new Traffimnagement ();

Department. ADD (New Bus ());

Department. ADD (New Train ());

Department. Accept (New Nationaldaynotifyvisitor ());

}

Results:

Bus Shijiazhuang to Xingtai -Yuan

During the national day, car tickets on the basis of the original price, floating 5%!

Train Shijiazhuang to Xingtai -¥

During the National day, train tickets on the basis of the original price, floating 3%!

(ix) The productivity has developed, the fares will fall!

New Plus Desire visitor.

public class Willvisitor:notifyvisitor

{

public override void Visit (Bus bus)

{

Bus. Showprice ();

Console.WriteLine ("Productivity has developed the people happy, car tickets on the basis of the original price, down 90%!" ");

}

public override void Visit (Train Train)

{

Train. Showprice ();

Console.WriteLine ("Productivity has developed the people happy, train tickets on the basis of the original price, down by 90%!" ");

}

}

(10) test

public void Testvisitor ()

{

Traffimnagement Department = new Traffimnagement ();

Department. ADD (New Bus ());

Department. ADD (New Train ());

Department. Accept (New Willvisitor ());

}

Results:

Bus Shijiazhuang to Xingtai -Yuan

Productivity developed people happy, car ticket on the basis of the original price, cut 90%!

Train Shijiazhuang to Xingtai -¥

Productivity developed the people happy, train tickets on the basis of the original price, lowered 90%!


The visitor pattern is implemented by the so-called double-distribution (double dispatch) to dynamically add new operations to classes on class hierarchies transparently at run time without changing the element class hierarchy. The so-called dual distribution, the visitor pattern, consists of two polymorphic distributions: The first is a polymorphic analysis of the Accept method; The second is a polymorphic analysis of the Visit method (overloading)
The biggest disadvantage of the visitor pattern is that extending the class hierarchy (adding a new element subclass) leads to a change in the visitor class, so the visitor pattern stabilizes the user element class layer, where operations often face frequent changes.

We need to add a visit function to the Notifyvisitor class when we need to increase the subclass of a vehicle, and each derived class of notifyvisitor must also be added.

Instance of a pattern--visitor instance

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.