. NET Design Mode visitor mode,. net Design Mode visitor

Source: Internet
Author: User

. NET Design Mode visitor mode,. net Design Mode visitor
1. Definition of visitor mode:

Indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on these elements without changing the element classes.

2. structure and role of the visitor mode:

 

1. Visitor abstracts the Visitor role and declares an access operation interface for the specific element role in the object structure. The name and parameters of this operation interface identify the specific element role that sends an access request to a specific visitor, so that the visitor can directly access it through the specific interface of this element role.
2. ConcreteVisitor. The Visitor role implements the interface declared by the Visitor.
3. Element defines an accept () that uses a Visitor as a parameter.
4. The specific elements of ConcreteElement implement the acceptance operation interface defined by the abstract Element.
5. ObjectStructure object roles, which are essential for using the visitor mode. It has the following features: it can enumerate its elements; it can provide a high-level interface to allow visitors to access its elements; if necessary, it can be designed as a composite object or an aggregation (such as a list or unordered set ).

3. Visitor mode implementation:

In our daily life, we will go to the hospital to see a doctor. When we register a doctor to find the attending doctor, we will open a ticket for you to pay for the medicine, the drug charge is collected by the specified doctor, and then you take the payment order to catch the pharmacist. Traditionally, we may write a price assignment class to determine the charge based on the name of the drug. Write a pharmacist to determine where to take the drug based on the name on the ticket. The Switch may be used for judgment, so that the code of the price-based staff and the pharmacy staff will be changed when the drug is added.

The purpose of the visitor mode is to encapsulate operations applied to a certain data structure element. Once these operations need to be modified, the data structure that accepts the operation can remain unchanged. Multiple Access methods are provided for different types of elements, and new operation methods can be added without modifying the original system. This is the motivation of the visitor mode.

/// <Summary> /// abstract Visitor /// </summary> public abstract class Visitor {protected string name {get; set;} public Visitor (string name) {this. name = name;} public abstract void visitor (MedicineA a); public abstract void visitor (MedicineB B);} // hovertree.com
/// <Summary> /// Visitor: price clerk /// </summary> public class Charger: Visitor {public Charger (string name): base (name) {} public override void visitor (MedicineA a) {Console. writeLine ("Price CLERK:" + this. name + "medication" +. getName () + "Price:" +. getPrice ();} public override void visitor (Fig B) {Console. writeLine ("Price CLERK:" + this. name + "medication" + B. getName () + "Price:" + B. getPrice () ;}// he asked hovertree.com
/// <Summary> // specific Visitor: Pharmacy worker // </summary> public class WorkerOfPharmacy: Visitor {public WorkerOfPharmacy (string name): base (name) {} public override void visitor (MedicineA a) {Console. writeLine ("pharmacy staff:" + this. name + ", take medicine:" +. getName ();} public override void visitor (Fig B) {Console. writeLine ("pharmacy staff:" + this. name + ", take medicine:" + B. getName () ;}// he asked hovertree.com
/// <Summary> /// abstract element: Medicine /// </summary> public abstract class Medicine {protected string name {get; set;} protected double price {get; set;} public Medicine (string name, double price) {this. name = name; this. price = price;} public string GetName () {return name;} public double GetPrice () {return price;} public void SetPrice (double price) {this. price = price;} public abstract void accept (Visitor visitor);} // He asked hovertree.com
/// <Summary> /// specific element: A name Medicine /// </summary> public class MedicineA: Medicine {public MedicineA (string name, double price ): base (name, price) {} public override void accept (Visitor visitor) {visitor. visitor (this) ;}// he asked hovertree.com
/// <Summary> /// specific element: B name Medicine /// </summary> public class MedicineB: Medicine {public MedicineB (string name, double price ): base (name, price) {} public override void accept (Visitor visitor) {visitor. visitor (this) ;}// he asked hovertree.com
/// <Summary> /// specific element: Ticket // </summary> public class Presciption {private List <Medicine> listmedicine = new List <Medicine> (); public void accpet (Visitor visitor) {foreach (var item in listmedicine) {item. accept (visitor) ;}} public void add (Medicine med) {listmedicine. add (med);} public void remove (Medicine med) {listmedicine. remove (med) ;}// he asked hovertree.com
/// <Summary> // C # design mode-Visitor mode // </summary> class Program {static void Main (string [] args) {// type of Medicine a = new Medicine A ("Medicine a", 10); Medicine B = new Medicine B ("Medicine B", 20 ); // Presciption presciption = new Presciption (); presciption. add (a); presciption. add (B); Visitor charger = new Charger ("Zhang San"); // clerk Visitor workerOfPharmacy = new WorkerOfPharmacy ("Li Si"); // delimiter presciption. accpet (charger); // price calculation Console. writeLine (); presciption. accpet (workerOfPharmacy); // take medicine} // He asked hovertree.com

 



Iv. Advantages and Disadvantages of visitor mode:

Advantages:
1. Makes it easier to add new access operations.
2. allows you to define the operation of the existing class hierarchy without modifying the existing class hierarchy.
3. Aggregate the access behavior of element objects into a visitor object, rather than scatter element classes.
Disadvantages:
1. It is very difficult to add new element classes. In the visitor mode, each addition of a new element class means that a new abstract operation should be added to the abstract visitor role, and corresponding specific operations should be added to each specific visitor class, it violates the requirements of the "Open and closed principle.
2. Destroy encapsulation. When the visitor mode is adopted, the encapsulation of the combination class will be broken.
3. Difficult to understand

5. application scenarios:

1. The classes corresponding to objects in the object structure are rarely changed, but new operations need to be defined on the object structure.
2. You need to perform many different and unrelated operations on the objects in an object structure, instead of causing these operations to "pollute" the classes of these objects, you do not want to modify these classes when adding new operations.

Http://www.cnblogs.com/roucheng/p/3521864.html

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.