// AOP example based on code by J. Hannemann and G. Kiczales import java.util.HashSet; import java.util.HashMap; // for associating observees with observers public abstract aspect ObserverPattern { protected interface Observer { void update(Observed s); } protected interface Observed {} // structure to associate observers with observed protected HashMap> ObvHM = new HashMap>(); protected HashSet getObservers(Observed s) { HashSet V = ObvHM.get(s); if (V==null) { V=new HashSet(); ObvHM.put(s,V); } return V; } public void addObserver(Observed s, Observer v) { getObservers(s).add(v); } public void removeObserver(Observed s, Observer v) { getObservers(s).remove(v); } abstract protected pointcut ObservedChange(Observed s); // add code to notify observers after each ObservedChange after(Observed s): ObservedChange(s) { for(Observer v:getObservers(s)) v.update(s); } }// aspect ObserverPattern // sample usage of abstract pattern: class stock { public double price; public final String symbol; public stock(String s, double p) { symbol = s; price = p; SymMap.put(s,this); } public void changePrice(double dp) { price += dp; } // global map from symbols to stock objects public static HashMap SymMap= new HashMap(); }//stock class investor { public final String name; // name of investor public HashMap investments; // maps symbols to num. shares public investor(String n) { name = n; investments = new HashMap(); } public void sell(String sym, int shares) { Integer s = investments.get(sym); if (s==null || s