aboutsummaryrefslogtreecommitdiffstats
path: root/docs/modules/ROOT/examples/observer
diff options
context:
space:
mode:
Diffstat (limited to 'docs/modules/ROOT/examples/observer')
-rw-r--r--docs/modules/ROOT/examples/observer/Button.java40
-rw-r--r--docs/modules/ROOT/examples/observer/ColorLabel.java34
-rw-r--r--docs/modules/ROOT/examples/observer/Demo.java29
-rw-r--r--docs/modules/ROOT/examples/observer/Display.java46
-rw-r--r--docs/modules/ROOT/examples/observer/Observer.java18
-rw-r--r--docs/modules/ROOT/examples/observer/Subject.java21
-rw-r--r--docs/modules/ROOT/examples/observer/SubjectObserverProtocol.java41
-rw-r--r--docs/modules/ROOT/examples/observer/SubjectObserverProtocolImpl.java31
-rw-r--r--docs/modules/ROOT/examples/observer/files.lst8
9 files changed, 268 insertions, 0 deletions
diff --git a/docs/modules/ROOT/examples/observer/Button.java b/docs/modules/ROOT/examples/observer/Button.java
new file mode 100644
index 000000000..79b33caa9
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/Button.java
@@ -0,0 +1,40 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+
+package observer;
+
+import java.awt.Color;
+import java.awt.event.ActionListener;
+import java.awt.event.ActionEvent;
+
+class Button extends java.awt.Button {
+
+ static final Color defaultBackgroundColor = Color.gray;
+ static final Color defaultForegroundColor = Color.black;
+ static final String defaultText = "cycle color";
+
+ Button(Display display) {
+ super();
+ setLabel(defaultText);
+ setBackground(defaultBackgroundColor);
+ setForeground(defaultForegroundColor);
+ addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ Button.this.click();
+ }
+ });
+ display.addToFrame(this);
+ }
+
+ public void click() {}
+}
diff --git a/docs/modules/ROOT/examples/observer/ColorLabel.java b/docs/modules/ROOT/examples/observer/ColorLabel.java
new file mode 100644
index 000000000..5709545f2
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/ColorLabel.java
@@ -0,0 +1,34 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+package observer;
+import java.awt.Color;
+import java.awt.Label;
+
+class ColorLabel extends Label {
+
+ ColorLabel(Display display) {
+ super();
+ display.addToFrame(this);
+ }
+
+ final static Color[] colors = {Color.red, Color.blue,
+ Color.green, Color.magenta};
+ private int colorIndex = 0;
+ private int cycleCount = 0;
+ void colorCycle() {
+ cycleCount++;
+ colorIndex = (colorIndex + 1) % colors.length;
+ setBackground(colors[colorIndex]);
+ setText("" + cycleCount);
+ }
+}
diff --git a/docs/modules/ROOT/examples/observer/Demo.java b/docs/modules/ROOT/examples/observer/Demo.java
new file mode 100644
index 000000000..03be6a614
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/Demo.java
@@ -0,0 +1,29 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+package observer;
+
+public class Demo {
+ public static void main(String[] args) {
+
+ Display display = new Display();
+ Button b1 = new Button(display);
+ Button b2 = new Button(display);
+ ColorLabel c1 = new ColorLabel(display);
+ ColorLabel c2 = new ColorLabel(display);
+ ColorLabel c3 = new ColorLabel(display);
+
+ b1.addObserver(c1);
+ b1.addObserver(c2);
+ b2.addObserver(c3);
+ }
+}
diff --git a/docs/modules/ROOT/examples/observer/Display.java b/docs/modules/ROOT/examples/observer/Display.java
new file mode 100644
index 000000000..67ed2cb5b
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/Display.java
@@ -0,0 +1,46 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+package observer;
+import java.awt.Frame;
+import java.awt.Panel;
+import java.awt.Container;
+import java.awt.Component;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.BorderLayout;
+
+/*
+ * Display is the container class that holds all the views of the
+ * colored number.
+ * In this demo, it holds buttons.
+ */
+
+class Display extends Panel {
+
+ protected Frame frame = new Frame("Subject/Observer Demo");
+
+ Display() {
+ frame.addWindowListener(new WindowAdapter() {
+ public void windowClosing(WindowEvent e) {System.exit(0);}
+ });
+
+ frame.add(this, BorderLayout.CENTER);
+ frame.pack();
+ frame.setVisible(true);
+ }
+
+ void addToFrame(Component c) {
+ add(c);
+ frame.pack();
+ }
+}
diff --git a/docs/modules/ROOT/examples/observer/Observer.java b/docs/modules/ROOT/examples/observer/Observer.java
new file mode 100644
index 000000000..2851ebe17
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/Observer.java
@@ -0,0 +1,18 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+package observer;
+
+interface Observer {
+ void setSubject(Subject s);
+ Subject getSubject();
+ void update();
+}
diff --git a/docs/modules/ROOT/examples/observer/Subject.java b/docs/modules/ROOT/examples/observer/Subject.java
new file mode 100644
index 000000000..d6c144e38
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/Subject.java
@@ -0,0 +1,21 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+package observer;
+import java.util.Vector;
+
+interface Subject {
+ void addObserver(Observer obs);
+ void removeObserver(Observer obs);
+ Vector getObservers();
+ Object getData();
+}
diff --git a/docs/modules/ROOT/examples/observer/SubjectObserverProtocol.java b/docs/modules/ROOT/examples/observer/SubjectObserverProtocol.java
new file mode 100644
index 000000000..05e54d76c
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/SubjectObserverProtocol.java
@@ -0,0 +1,41 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+package observer;
+
+import java.util.Vector;
+
+abstract aspect SubjectObserverProtocol {
+
+ abstract pointcut stateChanges(Subject s);
+
+ after(Subject s): stateChanges(s) {
+ for (int i = 0; i < s.getObservers().size(); i++) {
+ ((Observer)s.getObservers().elementAt(i)).update();
+ }
+ }
+
+ private Vector Subject.observers = new Vector();
+ public void Subject.addObserver(Observer obs) {
+ observers.addElement(obs);
+ obs.setSubject(this);
+ }
+ public void Subject.removeObserver(Observer obs) {
+ observers.removeElement(obs);
+ obs.setSubject(null);
+ }
+ public Vector Subject.getObservers() { return observers; }
+
+ private Subject Observer.subject = null;
+ public void Observer.setSubject(Subject s) { subject = s; }
+ public Subject Observer.getSubject() { return subject; }
+}
diff --git a/docs/modules/ROOT/examples/observer/SubjectObserverProtocolImpl.java b/docs/modules/ROOT/examples/observer/SubjectObserverProtocolImpl.java
new file mode 100644
index 000000000..2bc75918c
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/SubjectObserverProtocolImpl.java
@@ -0,0 +1,31 @@
+/*
+Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
+
+Use and copying of this software and preparation of derivative works based
+upon this software are permitted. Any distribution of this software or
+derivative works must comply with all applicable United States export control
+laws.
+
+This software is made available AS IS, and Xerox Corporation makes no warranty
+about the software, its performance or its conformity to any specification.
+*/
+
+package observer;
+
+import java.util.Vector;
+
+aspect SubjectObserverProtocolImpl extends SubjectObserverProtocol {
+
+ declare parents: Button implements Subject;
+ public Object Button.getData() { return this; }
+
+ declare parents: ColorLabel implements Observer;
+ public void ColorLabel.update() {
+ colorCycle();
+ }
+
+ pointcut stateChanges(Subject s):
+ target(s) &&
+ call(void Button.click());
+
+}
diff --git a/docs/modules/ROOT/examples/observer/files.lst b/docs/modules/ROOT/examples/observer/files.lst
new file mode 100644
index 000000000..9b800286e
--- /dev/null
+++ b/docs/modules/ROOT/examples/observer/files.lst
@@ -0,0 +1,8 @@
+ColorLabel.java
+Button.java
+Display.java
+Subject.java
+Observer.java
+SubjectObserverProtocol.java
+SubjectObserverProtocolImpl.java
+Demo.java