// computer.beh -- behavior definition for Computer example Computer { traversal allEquip(InventoryVisitor inv, PricingVisitor price) { to Equipment; } } PricingVisitor { before Equipment (@ total = total.add(host.get_netPrice()); @) before CompositeEquipment (@ total = total.add(host.get_discountPrice()); @) } Currency { (@ // a constructor to make things a little easier... public Currency(int d) { dollars = new Integer(d); } public Currency add(Currency c) { return new Currency(this.get_dollars().intValue() + c.get_dollars().intValue()); } @) } InventoryVisitor { before Equipment (@ inventory.accumulate(host); @) } Inventory { (@ public void accumulate(Equipment eq) { this.get_equipment().append(eq); } @) } Equipment_List { (@ public void append(Equipment eq) { Nonempty_Equipment_List neweq = new Nonempty_Equipment_List(eq, null); if (this.get_first() == null) { this.set_first(neweq); } else { Nonempty_Equipment_List e = this.get_first(); while (e.get_next() != null) e = e.get_next(); e.set_next(neweq); } } @) } Main { (@ static public void main(String args[]) throws Exception { Computer comp = Computer.parse(System.in); InventoryVisitor inv = new InventoryVisitor(new Inventory(new Equipment_List(null))); PricingVisitor price = new PricingVisitor(new Currency(0)); /* InventoryVisitor inv = InventoryVisitor.parse(""); PricingVisitor price = PricingVisitor.parse("$ 0"); */ comp.allEquip(inv, price); // Normally this would be done with g_print... System.out.println("Inventory:"); for (Nonempty_Equipment_List inventory = inv.get_inventory().get_equipment().get_first(); inventory != null; inventory = inventory.get_next()) { System.out.println(" " + inventory.get_it()); } System.out.println("Price " + price.get_total().get_dollars()); } @) }