carfield.com.hk ColorObserver.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.aspectj; /* -*- 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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ import java.awt.Color; import ca.ubc.cs.spl.pattern.library.ObserverProtocol; /** * Concretizes the observing relationship for &lt;code&gt;Point&lt;/code&gt; (subject) * and &lt;code&gt;Screen&lt;/code&gt; (observer). Color changes trigger updates. * * @author Gregor Kiczales * @author Jan Hannemann * @version 1.0, 05/13/02 * */ public aspect ColorObserver extends ObserverProtocol{ /** * Assings the &lt;code&gt;Subject&lt;/code&gt; role to the &lt;code&gt;Point&lt;/code&gt; class. * Roles are modeled as (empty) interfaces. */ declare parents: Point implements Subject; /** * Assings the &lt;code&gt;Observer&lt;/code&gt; role to the &lt;code&gt;Screen&lt;/code&gt; class. * Roles are modeled as (empty) interfaces. */ declare parents: Screen implements Observer; /** * Specifies the joinpoints that constitute a change to the * &lt;code&gt;Subject&lt;/code&gt;. Captures calls to &lt;code&gt;Point.setColor(Color) * &lt;/code&gt;. * * @param s the &lt;code&gt;Point&lt;/code&gt; acting as &lt;code&gt;Subject&lt;/code&gt; */ protected pointcut subjectChange(Subject s): call(void Point.setColor(Color)) &amp;&amp; target(s); /** * Defines how each &lt;code&gt;Observer&lt;/code&gt; is to be updated when a change * to a &lt;code&gt;Subject&lt;/code&gt; occurs. * * @param s the subject on which a change of interest occured * @param o the observer to be notifed of the change */ protected void updateObserver(Subject s, Observer o) { ((Screen)o).display(&quot;Screen updated because color changed.&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z CoordinateObserver.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.aspectj; /* -*- 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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ import ca.ubc.cs.spl.pattern.library.ObserverProtocol; /** * Concretizes the observing relationship for &lt;code&gt;Point&lt;/code&gt; (subject) * and &lt;code&gt;Screen&lt;/code&gt; (observer). Coordinate changes trigger updates. * * @author Gregor Kiczales * @author Jan Hannemann * @version 1.0, 05/13/02 */ public aspect CoordinateObserver extends ObserverProtocol{ /** * Assings the &lt;code&gt;Subject&lt;/code&gt; role to the &lt;code&gt;Point&lt;/code&gt; class. * Roles are modeled as (empty) interfaces. */ declare parents: Point implements Subject; /** * Assings the &lt;code&gt;Observer&lt;/code&gt; role to the &lt;code&gt;Screen&lt;/code&gt; class. * Roles are modeled as (empty) interfaces. */ declare parents: Screen implements Observer; /** * Specifies the joinpoints that constitute a change to the * &lt;code&gt;Subject&lt;/code&gt;. Captures calls to &lt;code&gt;Point.setX(int) * &lt;/code&gt; and &lt;code&gt;Point.setY(int)&lt;/code&gt;. * * @param s the &lt;code&gt;Point&lt;/code&gt; acting as &lt;code&gt;Subject&lt;/code&gt; */ protected pointcut subjectChange(Subject s): ( call(void Point.setX(int)) || call(void Point.setY(int)) ) &amp;&amp; target(s); /** * Defines how each &lt;code&gt;Observer&lt;/code&gt; is to be updated when a change * to a &lt;code&gt;Subject&lt;/code&gt; occurs. * * @param s the subject on which a change of interest occured * @param o the observer to be notifed of the change */ protected void updateObserver(Subject s, Observer o) { ((Screen)o).display(&quot;Screen updated because coordinates changed.&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Main.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ import java.awt.Color; /** * Implements the driver for the observer design pattern example.&lt;p&gt; * * Intent: &lt;i&gt;Define a one-to-many dependency between objects so that when one * object changes state, all its dependents are notified and updated * automatically&lt;/i&gt;&lt;p&gt; * * Participatng objects are &lt;code&gt;Point&lt;/code&gt; p and &lt;code&gt;Screen&lt;/code&gt; * s1, s2, s3, s4, and s5.&lt;p&gt; * * Three different kinds of observing relationships are realized: &lt;UL&gt; * &lt;LI&gt; &lt;code&gt;Screen&lt;/code&gt; s1 and s2 observe color changes of &lt;code&gt;Point * &lt;/code&gt; p. * &lt;LI&gt; &lt;code&gt;Screen&lt;/code&gt; s3 and s4 observe coordinate changes of &lt;code&gt; * Point&lt;/code&gt; p. * &lt;LI&gt; &lt;code&gt;Screen&lt;/code&gt; s5 observes the &lt;code&gt;display(String)&lt;/code&gt; * methods of &lt;code&gt;Screen&lt;/code&gt; s2 and s4. * &lt;/UL&gt; * * Every time an event of interest occurs, the observing &lt;code&gt;Screen&lt;/code&gt; * prints and appropriate message to stdout. &lt;p&gt; * * &lt;p&gt;This is the pure Java version.&lt;/i&gt;&lt;p&gt; * * The example illustrates that it is hard to * cleanly modularize the different observing relationships. The following * implementation issues have to be considered for the Java version: * &lt;UL&gt; * &lt;LI&gt; Observer and Subject can only be interfaces (as opposed to abstract * classes) if we do not want to restrict inhertance and thus code * reuse of existing classes completely. * &lt;LI&gt; As interfaces, we cannot attach default implementations for methods * like &lt;code&gt;attach()&lt;/code&gt;, &lt;code&gt;notify()&lt;/code&gt;, etc. Note that * these two problems only apply because Java does not offer multiple * inheritance. * &lt;LI&gt; Some implementation constraints are made implicit and are thus not * enforced: I.e., each Subject needs a field to store its observers * &lt;LI&gt; The classes that become subject and observer in the pattern context * need to be modified. In particular, subjects need to store the * mapping, implement the appropriate procedures. Observers need to * implement &lt;code&gt;update()&lt;/code&gt; * &lt;LI&gt; If a particular class takes part in more than one observing * relationship (as in this example), it is difficult to have both * notify/update mechanisms go through the same interface and yet * separate them cleanly. * &lt;/UL&gt; * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Point * @see Screen * @see Observer * @see Subject */ public class Main { /** * Implements the driver for the observer example. It creates five * &lt;code&gt;Screen&lt;/code&gt; objects and one &lt;code&gt;Point&lt;/code&gt; object * and sets the appropriate observing relationships (see above). * After the setup, the color of the point is changed, then it's * x-coordinate. &lt;p&gt; * The following results should be expected: &lt;OL&gt; * &lt;LI&gt; The color change should trigger s1 and s2 to each print an * appropriate message. * &lt;LI&gt; s2's message should trigger it's observer s5 to print * a message. * &lt;LI&gt; The coordinate change should trigger s3 and s4. * &lt;LI&gt; s4's message should trigger it's observer s5 again. */ public static void main(String argv[]) { Point p = new Point(5, 5, Color.blue); System.out.println(&quot;Creating Screen s1,s2,s3,s4,s5 and Point p&quot;); Screen s1 = new Screen(&quot;s1&quot;); Screen s2 = new Screen(&quot;s2&quot;); Screen s3 = new Screen(&quot;s3&quot;); Screen s4 = new Screen(&quot;s4&quot;); Screen s5 = new Screen(&quot;s5&quot;); System.out.println(&quot;Creating observing relationships:&quot;); System.out.println(&quot;- s1 and s2 observe color changes to p&quot;); System.out.println(&quot;- s3 and s4 observe coordinate changes to p&quot;); System.out.println(&quot;- s5 observes s2's and s4's display() method&quot;); p.attach(s1); p.attach(s2); p.attach(s3); p.attach(s4); s2.attach(s5); s4.attach(s5); System.out.println(&quot;Changing p's color:&quot;); p.setColor(Color.red); System.out.println(&quot;Changing p's x-coordinate:&quot;); p.setX(4); System.out.println(&quot;done.&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Observer.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ /** * Defines the method used to update &lt;i&gt;Observers&lt;i&gt;. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Subject */ public interface Observer { /** * Updates an &lt;i&gt;Observer&lt;/i&gt;. Uses the &lt;i&gt;push&lt;/i&gt; strategy (i.e. the * subject triggering the update passes itself as an argument). * * @param s the subject triggering the update */ public void update(Subject s); }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Point.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ import java.awt.Color; import java.util.HashSet; import java.util.Iterator; /** * Represents a point with x and y coordinates and a color. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 */ public class Point implements Subject { /** * stores the observers for this point (subject) */ private HashSet observers; /** * the point's x-coordinate */ private int x; /** * the point's y-coordinate */ private int y; /** * the point's current color */ private Color color; /** * Creates a new point object based on x and y coordinates and color. */ public Point(int x, int y, Color color) { this.x=x; this.y=y; this.color=color; this.observers = new HashSet(); } /** * Returns the point's current x-coordinate. * * @return the current x-coordinate */ public int getX() { return x; } /** * Returns the point's current y-coordinate. * * @return the current y-coordinate */ public int getY() { return y; } /** * Sets the current x-coordinate. * * @param x the new x-coordinate */ public void setX(int x) { this.x=x; notifyObservers(); } /** * Sets the current y-coordinate. * * @param y the new y-coordinate */ public void setY(int y) { this.y=y; notifyObservers(); } /** * Returns the point's current color. * * @return the current color */ public Color getColor() { return color; } /** * Sets the current color. * * @param color the new color */ public void setColor(Color color) { this.color=color; notifyObservers(); } public void attach(Observer o) { this.observers.add(o); } public void detach(Observer o) { this.observers.remove(o); } public void notifyObservers() { for (Iterator e = observers.iterator() ; e.hasNext() ;) { ((Observer)e.next()).update(this); } } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Screen.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ import java.util.HashSet; import java.util.Iterator; /** * Provides a means to output messages. This class is a placeholder for an * output device. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 */ public class Screen implements Subject, Observer { /** * stores the observers for this screen (subject) */ private HashSet observers; /** * the individual name of this screen object */ private String name; /** * creates a new &lt;code&gt;Screen&lt;/code&gt; object with the provided name. * * @param name the name for the new &lt;code&gt;Screen&lt;/code&gt; object */ public Screen(String s) { this.name = s; observers = new HashSet(); } /** * Prints the name of the &lt;code&gt;Screen&lt;/code&gt; object and the argument * string to stdout. * * @param s the string to print */ public void display (String s) { System.out.println(name + &quot;: &quot; + s); notifyObservers(); } public void attach(Observer o) { this.observers.add(o); } public void detach(Observer o) { this.observers.remove(o); } public void notifyObservers() { for (Iterator e = observers.iterator() ; e.hasNext() ;) { ((Observer)e.next()).update(this); } } public void update(Subject s) { display(&quot;update received from a &quot;+s.getClass().getName()+&quot; object&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z ScreenObserver.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.aspectj; /* -*- 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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ import ca.ubc.cs.spl.pattern.library.ObserverProtocol; /** * Concretizes the observing relationship for &lt;code&gt;Screen&lt;/code&gt; (subject) * and &lt;code&gt;Screen&lt;/code&gt; (observer). Calls of &lt;code&gt;Screen.display(String) * &lt;/code&gt; trigger updates. * * @author Gregor Kiczales * @author Jan Hannemann * @version 1.0, 05/13/02 */ public aspect ScreenObserver extends ObserverProtocol{ /** * Assings the &lt;code&gt;Subject&lt;/code&gt; role to the &lt;code&gt;Screen&lt;/code&gt; class. * Roles are modeled as (empty) interfaces. */ declare parents: Screen implements Subject; /** * Assings the &lt;code&gt;Observer&lt;/code&gt; role to the &lt;code&gt;Screen&lt;/code&gt; class. * Roles are modeled as (empty) interfaces. */ declare parents: Screen implements Observer; /** * Specifies the joinpoints that constitute a change to the * &lt;code&gt;Subject&lt;/code&gt;. Captures calls to &lt;code&gt;Screen.display(String) * &lt;/code&gt;. * * @param s the screen acting as &lt;code&gt;Subject&lt;/code&gt; */ protected pointcut subjectChange(Subject s): call(void Screen.display(String)) &amp;&amp; target(s); /** * Defines how each &lt;code&gt;Observer&lt;/code&gt; is to be updated when a change * to a &lt;code&gt;Subject&lt;/code&gt; occurs. * * @param s the subject on which a change of interest occured * @param o the observer to be notifed of the change */ protected void updateObserver(Subject s, Observer o) { ((Screen)o).display(&quot;Screen updated because screen displayed.&quot;); } } </TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z Subject.java 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.observer.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 &quot;License&quot;); 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 &quot;AS IS&quot; 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.patterns. * * Contributor(s): */ /** * Defines methods to attach and detach &lt;i&gt;Observers&lt;i&gt; to/from * &lt;i&gt;Subjects&lt;/i&gt;, and the &lt;code&gt;notifyObservers()&lt;/code&gt; method. * * @author Jan Hannemann * @author Gregor Kiczales * @version 1.0, 05/13/02 * * @see Observer */ public interface Subject { /** * Attaches an observer to this subject. * * @param o the observer to attach */ public void attach(Observer o); /** * Detaches an observer from this subject. * * @param o the observer to detach */ public void detach(Observer o); /** * Notifies all observers. */ public void notifyObservers(); }</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z files.lst 2004-03-24T16:00:00Z 2004-03-24T16:00:00Z <br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> 2004-03-24T16:00:00Z