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
- // Status
- Class Status
- {
- Public:
- Virtual ~ Status (){}
-
- Virtual void VisitSeed_A (Seed * elm ){}
-
- Virtual void VisitSeed_ B (Seed * elm ){}
-
- Protected:
- Status (){}
- };
-
- // Rain status
- Class Rain_Status: public Status
- {
- Public:
- Rain_Status (){}
-
- Virtual ~ Rain_Status (){}
-
- // The growth of A seed in the rain
- Virtual void VisitSeed_A (Seed * elm)
- {
- Cout <"Rain will visit Seed A..." < }
-
- // The Growth of B seeds in the rain Condition
- Virtual void VisitSeed_ B (Seed * elm)
- {
- Cout <"Rain will visit Seed B..." < }
- };
-
- // Sunshine status
- Class Sun_Status: public Status
- {
- Public:
- Sun_Status (){}
-
- Virtual ~ Sun_Status (){}
-
- // The growth of A seed in the sunlight
- Virtual void VisitSeed_A (Seed * elm)
- {
- Cout <"Sun will visit Seed A..." < }
-
- // The Growth of B seeds in the sunlight
- Virtual void VisitSeed_ B (Seed * elm)
- {
- Cout <"Sun will visit Seed B..." < }
- };
-
-
-
- // Seed
- Class Seed
- {
- Public:
- Virtual ~ Seed (){}
- Virtual void Accept (Status * vis) = 0;
-
- Protected:
- Seed (){}
- };
-
- // Seed A, which is assumed to be A normal seed
- Class Seed_A: public Seed
- {
- Public:
- Seed_A (){}
-
- ~ Seed_A (){}
-
- Void Accept (Status * vis)
- {
- Vis-> VisitSeed_A (this );
- }
- };
-
- // Seed B, which is assumed to be the seed from space
- Class Seed_ B: public Seed
- {
- Public:
- Seed_ B (){}
- ~ Seed_ B (){}
-
- Void Accept (Status * vis)
- {
- Vis-> VisitSeed_ B (this );
- }
- };
-
-
- // Object structure class, in order to compare different seeds
- Class ObjectStructure
- {
- Private:
- List Lseed;
-
- Public:
- // Add
- Void Attach (Seed * seed)
- {
- Lseed. push_back (seed );
- }
-
- // Delete
- Void Detach (Seed * seed)
- {
- Lseed. remove (seed );
- }
-
- // Show
- Void Display (Status * status)
- {
- List : Iterator it = lseed. begin ();
-
- For (it; it! = Lseed. end (); ++ it)
- {
- (* It)-> Accept (status );
- }
- }
- };
-
-
- // Test code
- Int main (int argc, char * argv [])
- {
- ObjectStructure obj;
-
- // Add two seeds to be compared
- Obj. Attach (new Seed_A ());
- Obj. Attach (new Seed_ B ());
-
- // View the status of two seeds in various states
- Obj. Display (new Rain_Status ());
-
- // Sun Satte
- Obj. Display (new Sun_Status ());
-
- Return 0;
- }
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.