Visitor design pattern lets you separate algorithms from the objects on which they operate.
The visitor pattern let you add behavior to a hierarchy of objects types without having to modify them. You can simply create a new visitor implementation where you can place the new behavior instead of injecting this new behavior into existing classes. Existing class objects are passed to one of the new visitor’s methods as an argument.
The implementation of the Visitor pattern provided by Java Architect is customizable in the GUI when applying the pattern.
Applying the Visitor pattern
The pattern can be applied either to an Interface or to a Class used as an inheritance root. The pattern will recursively scan the inheritance tree starting with the selected root to find all the sub-classes that the visitor will support. These sub classes are named 'visitable'.
![]() |
The selected 'root' class or interface must be modifiable along with its subclasses as the visitor pattern will have to modify them (defining the accept() method). |
-
In the model, select the existing root interface or class where you want to apply the visitor pattern.
-
From the contextual menu (rightclick) execute the
Java Architect →
Patterns → Create visitor command.
-
The command will pop the following GUI

-
Fill the 'visitor' fields in the form (the drawn figure in the dialog shows the role of each value):
-
Interface name: This is the name of the visitor interface, ie the interface that future visitor implementations will have to implement.
-
Create default implementation: When checked, Java Architect will also create a default implementation of the visitor. The default visitor does nothing but propagating the visit to the parent class visit method until the root type is reached. For the root type the default visitor returns
null
. -
Create empty implementation: When checked, Java Architect will create an empty implementation of the visitor. The empty visitor directly returns
null
for each visit method without propagating the visit to parent class visit method.
-
-
Fill the 'visitable' fields in the form
-
Create visitable Interface: When checked Java Architect will create a 'visitable' interface that defines the
accept()
method and make the visitable classes implements this interface. -
Visit method name: This is the name to be used for the
accept()
method. Default name is 'accept' which is a standard name for visitor pattern implementation and should be fine in most cases. -
Return type: This is the type to be returned by visit methods. Default type is
Object
.
-
-
Press the OK button to apply the pattern and create the Visitor class model.