C ++ design mode implementation-Visitor (Visitor) Mode

Source: Internet
Author: User

1. Visitor Mode

Definition: 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 classes of each element.

The structure is as follows:


Ii. Example

Assume that a scientific experiment is used to compare the growth of the two seeds in different environments.

Two seeds: Seed_A and Seed_ B ).

The growth environment is in rainy environment (Rain_Status), Sun_Status, and so on.

The structure is as follows:


The Code is as follows:

[Cpp]View plaincopy
  1. // Status
  2. Class Status
  3. {
  4. Public:
  5. Virtual ~ Status (){}
  6. Virtual void VisitSeed_A (Seed * elm ){}
  7. Virtual void VisitSeed_ B (Seed * elm ){}
  8. Protected:
  9. Status (){}
  10. };
  11. // Rain status
  12. Class Rain_Status: public Status
  13. {
  14. Public:
  15. Rain_Status (){}
  16. Virtual ~ Rain_Status (){}
  17. // The growth of A seed in the rain
  18. Virtual void VisitSeed_A (Seed * elm)
  19. {
  20. Cout <"Rain will visit Seed A..." < }
  21. // The Growth of B seeds in the rain Condition
  22. Virtual void VisitSeed_ B (Seed * elm)
  23. {
  24. Cout <"Rain will visit Seed B..." < }
  25. };
  26. // Sunshine status
  27. Class Sun_Status: public Status
  28. {
  29. Public:
  30. Sun_Status (){}
  31. Virtual ~ Sun_Status (){}
  32. // The growth of A seed in the sunlight
  33. Virtual void VisitSeed_A (Seed * elm)
  34. {
  35. Cout <"Sun will visit Seed A..." < }
  36. // The Growth of B seeds in the sunlight
  37. Virtual void VisitSeed_ B (Seed * elm)
  38. {
  39. Cout <"Sun will visit Seed B..." < }
  40. };
  41. // Seed
  42. Class Seed
  43. {
  44. Public:
  45. Virtual ~ Seed (){}
  46. Virtual void Accept (Status * vis) = 0;
  47. Protected:
  48. Seed (){}
  49. };
  50. // Seed A, which is assumed to be A normal seed
  51. Class Seed_A: public Seed
  52. {
  53. Public:
  54. Seed_A (){}
  55. ~ Seed_A (){}
  56. Void Accept (Status * vis)
  57. {
  58. Vis-> VisitSeed_A (this );
  59. }
  60. };
  61. // Seed B, which is assumed to be the seed from space
  62. Class Seed_ B: public Seed
  63. {
  64. Public:
  65. Seed_ B (){}
  66. ~ Seed_ B (){}
  67. Void Accept (Status * vis)
  68. {
  69. Vis-> VisitSeed_ B (this );
  70. }
  71. };
  72. // Object structure class, in order to compare different seeds
  73. Class ObjectStructure
  74. {
  75. Private:
  76. List Lseed;
  77. Public:
  78. // Add
  79. Void Attach (Seed * seed)
  80. {
  81. Lseed. push_back (seed );
  82. }
  83. // Delete
  84. Void Detach (Seed * seed)
  85. {
  86. Lseed. remove (seed );
  87. }
  88. // Show
  89. Void Display (Status * status)
  90. {
  91. List : Iterator it = lseed. begin ();
  92. For (it; it! = Lseed. end (); ++ it)
  93. {
  94. (* It)-> Accept (status );
  95. }
  96. }
  97. };
  98. // Test code
  99. Int main (int argc, char * argv [])
  100. {
  101. ObjectStructure obj;
  102. // Add two seeds to be compared
  103. Obj. Attach (new Seed_A ());
  104. Obj. Attach (new Seed_ B ());
  105. // View the status of two seeds in various states
  106. Obj. Display (new Rain_Status ());
  107. // Sun Satte
  108. Obj. Display (new Sun_Status ());
  109. Return 0;
  110. }

    Iii. Description

    1.First, we need to make it clear that the two seeds will not change easily, that is, only the common and space seeds. In other words, the data structure is relatively stable.

    2.It can be changed to the new State, such as the generation of an X-ray. To put it bluntly, the operation set can evolve freely.

    3.The advantage of this structure is that it is easy to add new operations. The disadvantage is that it is difficult to add new data structures because you need to add corresponding operations to every visitor.

    4.The seed growth chart has the following relationship with the structure of the visitor mode:

    Seed (seed) is equivalent to element, which is not quite changed.

    Status is equivalent to visitor, Which is variable and changeable. Note that each visitor must operate on all elements.

    5.In fact, we seldom use this mode because the data structure (element) remains unchanged.


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.