设计模式(23) 访问者模式
由于应用开发过程中先前完成的类型会因为需求变化(无论是业务功能,还是技术实现或是出于集成的需要)增加新的方法,如果直接在基类中增加新的方法,其派生类型可能需要相应进行比较繁琐的处理。而使用访问者模式可以做到在不改变既有类型层次的前提下,运行时动态为类型层次的每个类增加新的操作。 访问者模式 GOF对策略模式的描述为: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates... — Design Patterns : Elements of Reusable Object-Oriented Software UML类图 访问者模式包含五种角色: IVistor(抽象访问者):为该对象结构中具体元素角色声明一个访问操作接口。 ConcreteVisitor(具体访问者):每个具体...