// 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(0, new Stack(), sV); CheckingVisitor cV = new CheckingVisitor(iV,0); this.allWeights(cV, sV, iV ); return (cV.get_violations()); } @) } SummingVisitor { before Weight (@ total = total + host.get_i(); @) } CheckingVisitor { after Container (@ int initial = iV.get_initial(); int cap = host.get_capacity().get_i(); // violation of Law of Demeter // should use traversal int diff = iV.get_sV().get_total() - initial; if (diff > cap) { this.set_violations(get_violations() + 1); System.out.println(" total weight " + diff + " but limit is = " + cap + " OVER CAPACITY "); }; @) } InitialVisitor { // stack handling before Container (@ stack.push( new Integer(sV.get_total())); @) after Container (@ initial = ((Integer) stack.pop()).intValue(); @) } Main { (@ static public void main(String args[]) throws Exception { Container a = Container.parse(System.in); int result = a.checkCapacity(); } @) } // 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 = int. Weight = int. CheckingVisitor = InitialVisitor int. SummingVisitor = int. InitialVisitor = // takes care of computing initial value int // 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