Understanding the need of Visitor Pattern
作者:互联网
Understanding the need of Visitor Pattern
The visitor pattern is a solution to a more general design problem:
I have a hierarchy of different classes. Each class supports various common operations. We would now like to extend that hierarchy, without having to change the existing hierarchy (e.g. because it is defined by a library we do not have the source code to).
- We can add more classes, which must support all required operations.
- We can add more operations, which must be supported by all classes in the hierarchy.
When designing a hierarchy, we can choose which of the two extension directions we want to make easy. (Trying to do both is called the Expression Problem and is extremely tricky.)
Since this is a bit theoretic, let's use an example – various kinds of animals. Each animal has a name()
and makes a sound()
.
标签:operations,hierarchy,Visitor,classes,Pattern,need,more 来源: https://www.cnblogs.com/chucklu/p/14698913.html