Visitor pattern 1. Explain how the visitor pattern encapsulates the behavior to
ID: 3701314 • Letter: V
Question
Visitor pattern
1. Explain how the visitor pattern encapsulates the behavior to perform an operation into single classes.
2. Explain how the visitor pattern provides type-safe operations on each element in the object structure.
3. List the three locations where responsibility for traversal of the object structure can reside. Describe the operation of traversal for it location.
4. Describe the need to maintain state with a visitor.
5. Explain how the visitor pattern is an implementation of double dispatch.
Explanation / Answer
Answer)
Encapsulation of visitable objects:
The visitor defines the behavior and the visitable object defines the object structure. When the visitor needs to access the data kept by visitable object the pattern pushes and forces to expose from visitable objects using public defined methods . The data’s are exposed which are required in the visitor.
It is needed to maintain a state with the visitor because
It defines a separate object that is needed to implement an operation to perform elements of an object structure.
Helps the clients traverse the object structures and call the dispatching operation accept.
It also creates possibility to create new operations independently from the classes of an object structure by the help of new visitor objects.
The double-dispatch is the program where two indirect agents works together to provide two co-variant calls which works in perfect polymorphic concert with each other.
Hope this answers. :) Happy to help. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.