IIIA-Example : Observer

file: Main.java
includes all classes

Joinpointtypes

joinpointtype UpdateSignalling {
	// change of state that affects display of objects
}

Classes

abstract class Shape exhibits UpdateSignalling {
	pointcut UpdateSignalling :
		execution(void moveBy(int, int));
	 public abstract void moveBy(int dx, int dy);
}
class Point extends Shape exhibits UpdateSignalling {
	int x, y;
	pointcut UpdateSignalling :
		super || execution(void set*(int));
	public Point() {}  // needed for test
	public Point(int x, int y) {this.x = x; this.y = y;} // needed for test
	public int getX() { return x; }
	public int getY() { return y; }
	public void setX(int x) { this.x = x; }
	public void setY(int y) { this.y = y; }
	public void moveBy(int dx, int dy) {x+=dx; y+=dy;}
}
class Line extends Shape exhibits UpdateSignalling {
	private Point p1, p2;
	pointcut UpdateSignalling : super;
	public Point getP1() { return p1; }
	public Point getP2() { return p2; }
	public void setP1(Point p) {p1 = p;} // needed for test
	public void setP2(Point p) {p2 = p;} // needed for test
	public void moveBy(int dx, int dy) {
		p1.x += dx; p1.y += dy;
		p2.x += dx; p2.y += dy;
	}
}

Aspects

aspect Display advises UpdateSignalling {
	after (UpdateSignalling us) {
		System.out.println("\t[called]\t"+thisJoinPoint.getSignature());
		update();
	}
	static void update() {System.out.println("\t**\tDISPLAY IS UPDATET");}
}

Back to main page