Use this visitor to print an object's representation in a form that can be parsed back in to recreate the object.
The PrintVisitor has two constructors:
PrintVisitor(PrintWriter out) { ... } PrintVisitor() { this(new PrintWriter(System.out, true)); }
The PrintVisitor will send its output to the PrintWriter passed as the constructor's argument.
To actually do the printing, you pass a PrintVisitor to a traversal. Every object visited by the traversal will be printed to the PrintWriter associated with the PrintVisitor, possibly along with some of the syntax that appears in the program's class dictionary near the object. The PrintVisitor only guarentees that it will print all of an object's required syntax if it is combined with a traversal that visits all of that object's required parts. Users should also be aware that the strings created by the PrintVisitor will not be parseable if they do not contain all the required parts of the object graph.
For the PrintVisitor to print your object according to your formatting style, you need to specify some options in your class dictionary. Demeter describes 4 kinds of formating specifiers.
Use a DisplayVisitor to display all or part of an object's structure.
The DisplayVisitor has two constructors:
DisplayVisitor(PrintWriter out) { ... } DisplayVisitor() { this(new PrintWriter(System.out, true)); }
The DisplayVisitor will send its output to the PrintWriter passed as its argument.
To do the displaying, pass a DisplayVisitor to a traversal. Every object visited by the traversal will be displayed to the PrintWriter associated with the DisplayVisitor. The object will be displayed in a form designed to help visualise it, using indentation to show part containment.
The EqualVisitor has one constructor:
EqualVisitor(Object obj) { ... }
The EqualVisitor will compare the object it is traversing to the object passed as its argument. When the traversal is finished, call the method:
boolean get_is_equal()
to find out if the objects were equal or not.
Finally, each EqualVisitor should only be used to for one comparison. That is, make a new instance of EqualVisitor each time you need to compare a part of your program's object graph. This is because the EqualVisitor needs to maintain some internal state as part of the comparing process.
Use this visitor to recursively copy all or part of an object, specified by a traversal.
The CopyVisitor has one constructor:
CopyVisitor(Class cl) { ... }
The Class argument should be the class of the object to be copied. For example, if you want to use the CopyVisitor to copy part of an object of class Company, you would instantiate it like:
CopyVisitor cv = new CopyVisitor(Company.class);
To actually do the copying, you pass the CopyVisitor object to a traversal method. Every object visited by the traversal will be copied, and every object that is not visited will be set to null in the copy. Be aware that, because of these semantics, it is easy to create objects that are not consistant with a program's class dictionary (simply by using a CopyVisitor with a traversal that bypasses a required part).
Once the copying is done, the
Object get_copy()
method on the CopyVisitor can be used to get the newly constructed copy.
Finally, each CopyVisitor should only be used to make one copy. That is, make a new instance of CopyVisitor each time you need to copy a part of your program's object graph. This is because the CopyVisitor needs to maintain some internal state as part of the copying process.
The TraceVisitor is primarily a debugging tool. It has two constructors:
TraceVisitor(PrintWriter out) { ... } TraceVisitor() { this(new PrintWriter(System.out, true)); }
The TraceVisitor will send its output to the PrintWriter passed as its argument.
To actually generate a trace, you pass a TraceVisitor to a traversal. The TraceVisitor will hierarchically display all the before and after methods that would be activated by that traversal.