// behavior Container { traversal allWeights( CheckingVisitor cV, SummingVisitor sV, InitialVisitor iV ) { to Weight;} (@ // traverse while counting and summing int checkCapacity() throws Exception { // adaptive specification of objects using parsing SummingVisitor sV = SummingVisitor.parse("0"); InitialVisitor iV = new InitialVisitor(null, new Stack(), sV); CheckingVisitor cV = new CheckingVisitor(iV,new Integer(0)); this.allWeights(cV, sV, iV ); return (cV.get_violations().intValue()); } @) } SummingVisitor { before Weight (@ System.out.println("sum " + total.intValue()); total = new Integer(total.intValue() + host.get_i().intValue()); @) } CheckingVisitor { before Container (@ System.out.println(" start new container "); @) after Container (@ Integer initial = iV.get_initial(); int cap = host.get_capacity().get_i().intValue(); // violation of Law of Demeter // should use traversal int diff = iV.get_sV().get_total().intValue() - initial.intValue(); if (diff > cap) { this.set_violations(new Integer(get_violations().intValue() + 1)); System.out.println(" total weight " + diff + " but limit is = " + cap + " OVER CAPACITY "); }; System.out.println(" end container "); @) } InitialVisitor { // stack handling before Container (@ stack.push(sV.get_total()); @) after Container (@ initial = (Integer) stack.pop(); @) } Main { (@ static public void main(String args[]) throws Exception { Runtime rt = Runtime.getRuntime(); rt.traceMethodCalls(true); Container a = Container.parse(System.in); int result = a.checkCapacity(); if(result == 2) { System.out.println("SUCCESS"); } else { System.out.println("FAILURE"); } System.out.println("done "); } @) } // solving the capacity checking problem from hw 2 // without modifying the host // visitor has own stack to keep track of initial value // class dictionary (@ import java.util.*; @) Container = "(" List(Item) Capacity ")". Item : Container | Simple. List(S) ~ {S}. Simple = Ident Weight. Capacity = Integer. Weight = Integer. CheckingVisitor = InitialVisitor Integer. SummingVisitor = Integer. InitialVisitor = // takes care of computing initial value Integer // result for communication // with checking visitor Stack SummingVisitor. Main = . // input ( //begin container 1 apple 1 //item ( //begin container 2 pencil 1 //item ( //begin container 3 apple 1 //item 1) //capacity orange 1 1) //capacity orange 1 kiwi 1 5) //capacity