Pattern > Créateurs

Pattern > Structuraux

Pattern > Comportementaux

Références

L'actualité

Librairie

L'information

Design pattern > Comportementaux > Visitor

Ce patron représente une opération à effectuer sur un ensemble d'objets. Permet de modifier l'opération sans changer l'objet concerné ni la structure. Selon ce patron, les objets à modifier sont passés en paramètre à une classe tierce qui effectuera des modifications. Une classe abstraite Visitor définit l'interface de la classe tierce. Ce patron est utilisé notamment pour manipuler un jeu d'objets, où les objets peuvent avoir différentes interfaces, qui ne peuvent pas être modifiés.


Visitor (Visitor)
  • declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the elements directly through its particular interface
ConcreteVisitor (IncomeVisitor, VacationVisitor)
  • implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class or object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.
Element (Element)
  • defines an Accept operation that takes a visitor as an argument.
ConcreteElement (Employee)
  • implements an Accept operation that takes a visitor as an argument
ObjectStructure (Employees)
  • can enumerate its elements
  • may provide a high-level interface to allow the visitor to visit its elements
  • may either be a Composite (pattern) or a collection such as a list or a set
			
Clerk Hank's new income: $27,500.00
Director Elly's new income: $38,500.00
President Dick's new income: $49,500.00

Clerk Hank's new vacation days: 17
Director Elly's new vacation days: 19
President Dick's new vacation days: 24