Visitor-Visitor Mode

Source: Internet
Author: User

Visitor-Visitor Mode
Purpose
Without changing the classes in a hierarchy, define new operations that act on each element of the class.
CaseThere are different device Equipment, such as Chassis and FloppyDisk. Some people want to know its performance, but some want to know its price, and some may want to know other information, such as manufacturer and manufacturing materials. However, when an Equipment class is defined and a subclass is defined, you need to change all classes to add new operations. In this case, you can consider using the Visitor mode. In the Visitor mode, you only need to provide an accept (Visitor *) method in Equiment, and pass the information to the visitor object through the Visitor method of the Visitor class:

Equipment provides an accept operation so that it can work with a visitor:

 
 
  1. class Equipment
  2. {
  3. public:
  4. virtual void accept(Visitor* v) = 0;
  5. };
Perform the accept operation in the subclass:
 
 
  1. class FloppyDisk : public Equipment
  2. {
  3. public:
  4. virtual void accept(Visitor* v);
  5. };
  6. void FlopyyDisk::accept(Visitor* v)
  7. {
  8. v->visit(this);
  9. }
For example, if Chassis has sub-devices, you need to independently call the accept operation on each sub-device:
 
 
  1. class Chassis : public Equipment
  2. {
  3. public:
  4. virtual void accept(Visitor* v);
  5. private:
  6. std::vector m_parts;
  7. };
  8. void Chassis::accept(Visitor* v)
  9. {
  10. for(int i = 0; i < m_parts.size(); ++i)
  11. m_parts[i]->accept(v);
  12. v->visit(this);
  13. }
Visitor provides methods to access the Equipment object:
 
 
  1. class Visitor
  2. {
  3. public:
  4. virtual void visit(FloppyDisk* floppyDisk) = 0;
  5. virtual void visit(Chassis* chassis) = 0;
  6. };
Subclass mainly redefines two methods to get the desired results.
ApplicabilityAn object structure contains many class objects which have different interfaces and want to perform operations on these objects dependent on their specific classes. You need to perform many different and unrelated operations on the objects in an object structure, and want to avoid these operations from polluting the class of the objects. Classes that define the object structure are rarely changed, but new operations are often needed in this structure.

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.