RSS feed
[root]
/
document
/
software design
/
Design pattern example in aspect programming
/
java
/
visitor
/
examples
login:
password:
title search:
Search this site
Enter your search terms
Web
www.carfield.com.hk
Submit search form
Prev
Next
Wed Mar 24 16:00:00 GMT 2004
Main
package examples.visitor.java; /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * This file is part of the design patterns project at UBC * * The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * either http:*www.mozilla.org/MPL/ or http:*aspectj.org/MPL/. * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is ca.ubc.cs.spl.pattern. * * Contributor(s): */ /** * Implements the driver for the Visitor design pattern example.<p> * * Intent: <i>Represents 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</i><p> * * Participating classes are <code>SumVisitor</code> and * <code>TraversalVisitor</code> as <i>ConcreteVisitor</i>s, implementing the * <code>NodeVisitor</code> interface. <BR> * <code>RegularNode</code> and <code>Leaf</code> are <i>ConcreteElement</i>s, * implementing the <code>Node</code> interface. <p> * * In this example, Node, RegularNode and Leaf build up a binary tree that has * int values as leafs. SumVisitor is a NodeVisitor that collects the sum of * elements in the leafs (should be 6). TraversalVisitor is a visitor that * collects a description of the tree like {{1,2},3} * * <p><i>This is the Java version.</i><p> * * Note that <UL> * <LI> Every visitor (even the inteface) has to know of each possible element * type in the object structure. * <LI> Nodes need to know of the visitor interface; they have to implement the * accept(NodeVisitor) method. * </UL> * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 06/13/02 * * @see Node * @see RegularNode * @see Leaf * @see NodeVisitor * @see SumVisitor * @see TraversalVisitor */ public class Main { /** * Implements the driver for the Visitor design pattern example.<p> * * In this example, Node, RegularNode and Leaf build up a binary tree that has * int values as leafs. SumVisitor is a NodeVisitor that collects the sum of * elements in the leafs (should be 6). TraversalVisitor is a visitor that * collects a description of the tree like {{1,2},3} * * @param args the command-line parameters, unused */ public static void main(String[] args) { System.out.println("Building the tree (1): leaves"); Leaf one = new Leaf(1); Leaf two = new Leaf(2); Leaf three = new Leaf(3); System.out.println("Building the tree (1): regular nodes"); RegularNode regN = new RegularNode(one, two); RegularNode root = new RegularNode(regN, three); System.out.println("The tree now looks like this: "); System.out.println(" regN "); System.out.println(" / \\ "); System.out.println(" regN 3 "); System.out.println(" / \\ "); System.out.println(" 1 2 "); System.out.println("Visitor 1: SumVisitor, collects the sum of leaf"); System.out.println("values. Result should be 6."); SumVisitor sumVisitor = new SumVisitor(); root.accept(sumVisitor); System.out.println(sumVisitor.report()); System.out.println("Visitor 2: TraversalVisitor, collects a tree"); System.out.println("representation. Result should be {{1,2},3}."); TraversalVisitor traversalVisitor = new TraversalVisitor(); root.accept(traversalVisitor); System.out.println(traversalVisitor.report()); } }
(google search)
(amazon search)
1
2
3
second
download zip of files only
Prev
Next