carfield.com.hkColorObserver.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>Point</code> (subject)
* and <code>Screen</code> (observer). Color changes trigger updates.
*
* @author Gregor Kiczales
* @author Jan Hannemann
* @version 1.0, 05/13/02
*
*/
public aspect ColorObserver extends ObserverProtocol{
/**
* Assings the <code>Subject</code> role to the <code>Point</code> class.
* Roles are modeled as (empty) interfaces.
*/
declare parents: Point implements Subject;
/**
* Assings the <code>Observer</code> role to the <code>Screen</code> class.
* Roles are modeled as (empty) interfaces.
*/
declare parents: Screen implements Observer;
/**
* Specifies the joinpoints that constitute a change to the
* <code>Subject</code>. Captures calls to <code>Point.setColor(Color)
* </code>.
*
* @param s the <code>Point</code> acting as <code>Subject</code>
*/
protected pointcut subjectChange(Subject s): call(void Point.setColor(Color)) && target(s);
/**
* Defines how each <code>Observer</code> is to be updated when a change
* to a <code>Subject</code> 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("Screen updated because color changed.");
}
}
</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:00ZCoordinateObserver.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>Point</code> (subject)
* and <code>Screen</code> (observer). Coordinate changes trigger updates.
*
* @author Gregor Kiczales
* @author Jan Hannemann
* @version 1.0, 05/13/02
*/
public aspect CoordinateObserver extends ObserverProtocol{
/**
* Assings the <code>Subject</code> role to the <code>Point</code> class.
* Roles are modeled as (empty) interfaces.
*/
declare parents: Point implements Subject;
/**
* Assings the <code>Observer</code> role to the <code>Screen</code> class.
* Roles are modeled as (empty) interfaces.
*/
declare parents: Screen implements Observer;
/**
* Specifies the joinpoints that constitute a change to the
* <code>Subject</code>. Captures calls to <code>Point.setX(int)
* </code> and <code>Point.setY(int)</code>.
*
* @param s the <code>Point</code> acting as <code>Subject</code>
*/
protected pointcut subjectChange(Subject s):
( call(void Point.setX(int)) ||
call(void Point.setY(int)) ) && target(s);
/**
* Defines how each <code>Observer</code> is to be updated when a change
* to a <code>Subject</code> 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("Screen updated because coordinates changed.");
}
}
</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:00ZMain.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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.<p>
*
* Intent: <i>Define a one-to-many dependency between objects so that when one
* object changes state, all its dependents are notified and updated
* automatically</i><p>
*
* Participatng objects are <code>Point</code> p and <code>Screen</code>
* s1, s2, s3, s4, and s5.<p>
*
* Three different kinds of observing relationships are realized: <UL>
* <LI> <code>Screen</code> s1 and s2 observe color changes of <code>Point
* </code> p.
* <LI> <code>Screen</code> s3 and s4 observe coordinate changes of <code>
* Point</code> p.
* <LI> <code>Screen</code> s5 observes the <code>display(String)</code>
* methods of <code>Screen</code> s2 and s4.
* </UL>
*
*
* Every time an event of interest occurs, the observing <code>Screen</code>
* prints and appropriate message to stdout.
*
* <p><i>This is the AspectJ version.</i><p>
*
* @author Gregor Kiczales
* @author Jan Hannemann
* @version 1.0, 05/13/02
*
* @see Point
* @see Screen
*/
public class Main {
/**
* Implements the driver for the observer example. It creates five
* <code>Screen</code> objects and one <code>Point</code> object
* and sets the appropriate observing relationships (see above).
* After the setup, the color of the point is changed, then it's
* x-coordinate. <p>
* The following results should be expected: <OL>
* <LI> The color change should trigger s1 and s2 to each print an
* appropriate message.
* <LI> s2's message should trigger it's observer s5 to print
* a message.
* <LI> The coordinate change should trigger s3 and s4.
* <LI> s4's message should trigger it's observer s5 again.
* </OL>
*/
public static void main(String argv[]) {
Point p = new Point(5, 5, Color.blue);
System.out.println("Creating Screen s1,s2,s3,s4,s5 and Point p");
Screen s1 = new Screen("s1");
Screen s2 = new Screen("s2");
Screen s3 = new Screen("s3");
Screen s4 = new Screen("s4");
Screen s5 = new Screen("s5");
System.out.println("Creating observing relationships:");
System.out.println("- s1 and s2 observe color changes to p");
System.out.println("- s3 and s4 observe coordinate changes to p");
System.out.println("- s5 observes s2's and s4's display() method");
ColorObserver.aspectOf().addObserver(p, s1);
ColorObserver.aspectOf().addObserver(p, s2);
CoordinateObserver.aspectOf().addObserver(p, s3);
CoordinateObserver.aspectOf().addObserver(p, s4);
ScreenObserver.aspectOf().addObserver(s2, s5);
ScreenObserver.aspectOf().addObserver(s4, s5);
System.out.println("Changing p's color:");
p.setColor(Color.red);
System.out.println("Changing p's x-coordinate:");
p.setX(4);
System.out.println("done.");
}
}
</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:00ZPoint.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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;
/**
* Represents a point with x and y coordinates and a color.
*
* @author Gregor Kiczales
* @author Jan Hannemann
* @version 1.0, 05/13/02
*
*/
public class Point {
/**
* 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;
}
/**
* 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; }
/**
* Sets the current y-coordinate.
*
* @param y the new y-coordinate
*/
public void setY(int y) { this.y=y; }
/**
* 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; }
}
</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:00ZScreen.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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):
*/
/**
* Provides a means to output messages. This class is a placeholder for an
* output device.
*
* @author Gregor Kiczales
* @author Jan Hannemann
* @version 1.0, 05/13/02
*
*/
public class Screen {
/**
* the individual name of this screen object
*/
private String name;
/**
* creates a new <code>Screen</code> object with the provided name.
*
* @param name the name for the new <code>Screen</code> object
*/
public Screen(String s) {
this.name = s;
}
/**
* Prints the name of the <code>Screen</code> object and the argument
* string to stdout.
*
* @param s the string to print
*/
public void display (String s) {
System.out.println(name + ": " + 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:00ZScreenObserver.java2004-03-24T16:00:00Z2004-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 "License"); 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 "AS IS" 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 <code>Screen</code> (subject)
* and <code>Screen</code> (observer). Calls of <code>Screen.display(String)
* </code> trigger updates.
*
* @author Gregor Kiczales
* @author Jan Hannemann
* @version 1.0, 05/13/02
*/
public aspect ScreenObserver extends ObserverProtocol{
/**
* Assings the <code>Subject</code> role to the <code>Screen</code> class.
* Roles are modeled as (empty) interfaces.
*/
declare parents: Screen implements Subject;
/**
* Assings the <code>Observer</code> role to the <code>Screen</code> class.
* Roles are modeled as (empty) interfaces.
*/
declare parents: Screen implements Observer;
/**
* Specifies the joinpoints that constitute a change to the
* <code>Subject</code>. Captures calls to <code>Screen.display(String)
* </code>.
*
* @param s the screen acting as <code>Subject</code>
*/
protected pointcut subjectChange(Subject s):
call(void Screen.display(String)) && target(s);
/**
* Defines how each <code>Observer</code> is to be updated when a change
* to a <code>Subject</code> 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("Screen updated because screen displayed.");
}
}
</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:00Zfiles.lst2004-03-24T16:00:00Z2004-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