Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2010-04-21 12:18:00 -0400
committerStephan Herrmann2010-04-21 12:18:00 -0400
commit3605c8228f8610fcd3ab135dccf7a9e963766137 (patch)
treebaaad39732000f6f98038eb3c2c254cb91850e14 /othersrc/otdt-examples/OTSample-Observer-src
parentd5ab1505dee109bbb1aac3d6e5312d3560a876ea (diff)
downloadorg.eclipse.objectteams-3605c8228f8610fcd3ab135dccf7a9e963766137.tar.gz
org.eclipse.objectteams-3605c8228f8610fcd3ab135dccf7a9e963766137.tar.xz
org.eclipse.objectteams-3605c8228f8610fcd3ab135dccf7a9e963766137.zip
initial contribution "Object Teams Examples" as approved in CQ 3793
Diffstat (limited to 'othersrc/otdt-examples/OTSample-Observer-src')
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/.classpath7
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/.project18
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/observer-zip.jardesc10
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/Main.java97
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLibrary.java49
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLine.java57
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Book.java50
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookCopy.java68
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookManager.java69
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Person.java34
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/pattern/ObserverPattern.java106
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Area.java24
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Point.java28
-rw-r--r--othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Polyline.java44
14 files changed, 661 insertions, 0 deletions
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/.classpath b/othersrc/otdt-examples/OTSample-Observer-src/.classpath
new file mode 100644
index 000000000..bcc3bc2b8
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+ <classpathentry kind="con" path="OTRE"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/.project b/othersrc/otdt-examples/OTSample-Observer-src/.project
new file mode 100644
index 000000000..37d4665f8
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/.project
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>OTSample-Observer-src</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.objectteams.otdt.builder.OTJBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.objectteams.otdt.OTJavaNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/observer-zip.jardesc b/othersrc/otdt-examples/OTSample-Observer-src/observer-zip.jardesc
new file mode 100644
index 000000000..2fc91c671
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/observer-zip.jardesc
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<jardesc>
+ <jar path="org.eclipse.objectteams.otdt.samples/samples/observer.zip"/>
+ <options buildIfNeeded="true" compress="true" descriptionLocation="/OTSample-Observer-src/observer-zip.jardesc" exportErrors="false" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="true"/>
+ <storedRefactorings deprecationInfo="true" structuralOnly="false"/>
+ <selectedProjects/>
+ <selectedElements exportClassFiles="false" exportJavaFiles="true" exportOutputFolder="false">
+ <javaElement handleIdentifier="=OTSample-Observer-src"/>
+ </selectedElements>
+</jardesc>
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/Main.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/Main.java
new file mode 100644
index 000000000..777735124
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/Main.java
@@ -0,0 +1,97 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.application;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.eclipse.objectteams.example.observer.library.*;
+import org.eclipse.objectteams.example.observer.point_n_line.*;
+
+
+/**
+ * Simple main program: create some objects, invoke modifications and watch the result.
+ */
+public class Main {
+
+ public static void main (String[] args) {
+ // --- Testing ObserveLibrary: ---
+ ObserveLibrary ol = new ObserveLibrary();
+ ol.activate();
+
+ Person jack = new Person("Jack");
+ Book gof = new Book("Design Patterns", "GoF", "1234");
+ Book etl = new Book("Eiffel The Language", "BM", "345");
+ BookCopy gof1 = new BookCopy(gof);
+ BookCopy gof2 = new BookCopy(gof);
+ BookCopy etl1 = new BookCopy(etl);
+ BookCopy etl2 = new BookCopy(etl);
+
+ printBorrow(gof1, jack);
+ printBorrow(etl2, jack);
+
+ BookManager manager = new BookManager();
+ manager.printView();
+
+ manager.buy(gof1);
+ manager.buy(gof2);
+ manager.buy(etl1);
+ manager.buy(etl2);
+
+ manager.printView();
+
+ System.out.println("===> return gof1, borrow etl1");
+ gof1.returnIt();
+ printBorrow(etl1, jack);
+ manager.printView();
+
+ System.out.println("===> test unregistering:");
+ manager.drop(etl2);
+ etl2.returnIt();
+ manager.printView();
+ manager.buy(etl2);
+ manager.printView();
+
+ // --- Testing ObserveLine: ---
+ testPointNLine();
+ }
+
+ static void printBorrow(BookCopy bc, Person p) {
+ System.out.println(bc+" is borrowed by "+bc.borrow(p));
+ }
+
+ /**
+ * Perform some tests: create an Area and a Polyline.
+ * After drawing the line, perform some changes
+ *
+ */
+ static void testPointNLine() {
+ ObserveLine observing = new ObserveLine();
+ observing.activate(); // needed to make callin bindings effective.
+ Area area = new Area();
+ Polyline line = new Polyline();
+ area.draw(line);
+ // single point:
+ line.addPoint(new Point(0,0));
+ // list of points:
+ List<Point> points = Arrays.asList( new Point[] {
+ new Point(1,0), new Point(1,1), new Point(2,2)
+ });
+ // bulk-operation:
+ line.addPoints(points); // causes re-entrance (but not of observer! ;)
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLibrary.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLibrary.java
new file mode 100644
index 000000000..390c9d69a
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLibrary.java
@@ -0,0 +1,49 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.application;
+
+import org.eclipse.objectteams.example.observer.pattern.ObserverPattern;
+
+import base org.eclipse.objectteams.example.observer.library.BookManager;
+import base org.eclipse.objectteams.example.observer.library.BookCopy;
+
+/**
+ * This is a team class which is used as a connector, meaning, it contains
+ * just bindings, no implementation.
+ * The abstract modifier of superclass is dropped,
+ * because abstract methods are defined by deployment (using callout-bindings).
+ */
+public team class ObserveLibrary extends ObserverPattern {
+
+ public class Observer playedBy BookManager {
+
+ // Callout method binding: bind an action to the update event.
+ update -> updateView;
+
+ /* -------------------------------------------------------------- */
+
+ // Callin method bindings: bind events to trigger the start/stop operations.
+ start <- after buy;
+ stop <- before drop;
+ }
+
+ public class Subject playedBy BookCopy {
+
+ // Callin method binding: bind events to trigger the notification mechanism.
+ changeOp <- after returnIt, borrow;
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLine.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLine.java
new file mode 100644
index 000000000..234653baa
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/application/ObserveLine.java
@@ -0,0 +1,57 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.application;
+
+import org.eclipse.objectteams.example.observer.pattern.ObserverPattern;
+
+import base org.eclipse.objectteams.example.observer.point_n_line.Polyline;
+import base org.eclipse.objectteams.example.observer.point_n_line.Area;
+
+/**
+ * This class applies the observer pattern to a set of drawing elements.
+ * One could easily think of a canvas that should react to changes of
+ * its figures.
+ */
+public team class ObserveLine extends ObserverPattern {
+
+ /**
+ * Elements to be observed are simple poly-lines.
+ */
+ protected class Subject playedBy Polyline {
+ // bindings only:
+ changeOp <- after addPoint;
+ changeOpMany <- replace addPoints;
+ toString => toString; // callout overrides existing method
+ }
+ protected class Observer playedBy Area {
+ /**
+ * Only print out that a change happend and some action should be taken.
+ * (this method implements the inherited abstract method, instead of
+ * binding it to a base method).
+ *
+ * @param s the element that has changed.
+ */
+ protected void update(Subject s) {
+ System.out.println("Observing: "+s);
+ }
+ /**
+ * Bind the trigger to start the protocol:
+ * only elements that are drawn need to participate in the observer pattern.
+ */
+ start <- after draw;
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Book.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Book.java
new file mode 100644
index 000000000..d65c0696f
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Book.java
@@ -0,0 +1,50 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.library;
+
+import java.util.LinkedList;
+
+/**
+ * A title listed in the catalogue.
+ */
+public class Book {
+
+ private String name;
+ private String author;
+ private String isbn;
+ private LinkedList<BookCopy> copies = new LinkedList<BookCopy>();
+
+ public Book (String name, String author, String isbn) {
+ this.name = name;
+ this.author = name;
+ this.isbn = isbn;
+ }
+
+ public String getName () { return name ; }
+ public String getAuthor () { return author; }
+ public String getISBN () { return isbn; }
+
+ /**
+ * When a new copy of this book is acquired, use this method to register.
+ * @param bc
+ * @return number of registered copies after the operation.
+ */
+ public int registerCopy (BookCopy bc) {
+ copies.add(bc);
+ return copies.size();
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookCopy.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookCopy.java
new file mode 100644
index 000000000..59d4965f5
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookCopy.java
@@ -0,0 +1,68 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.library;
+
+/**
+ * Instances represent physical books in the shelf.
+ */
+public class BookCopy {
+
+ private Book theBook; // the title of which this is a copy
+ private Person borrower = null;
+ private int nr;
+
+ public BookCopy (Book ofBook) {
+ theBook = ofBook;
+ nr = ofBook.registerCopy(this);
+ }
+
+ /**
+ * When a person borrows a book, record who is the borrower.
+ * @param who
+ * @return the borrower
+ */
+ public Person borrow (Person who) {
+ borrower = who;
+ return borrower;
+ }
+
+ /**
+ * (Unfortunately cannot name a method "return" ;-)
+ * A book copy that was out is returned.
+ */
+ public void returnIt () {
+ borrower = null;
+ }
+
+ /**
+ * Is it available for borrowing?
+ */
+ public boolean isAvail () {
+ return borrower == null;
+ }
+
+ /**
+ * The ID of a book copy gives the title and a serial number.
+ */
+ public String getID () {
+ return "'"+theBook.getName() + "'#" + nr;
+ }
+
+ public String toString () {
+ return getID();
+ }
+} \ No newline at end of file
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookManager.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookManager.java
new file mode 100644
index 000000000..71139106f
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/BookManager.java
@@ -0,0 +1,69 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.library;
+
+import java.util.Arrays;
+import java.util.HashMap;
+
+/**
+ * Central class of a simplistic library-system.
+ * The book manager keeps a set of books.
+ */
+public class BookManager {
+
+ private HashMap<String, String> view = new HashMap<String, String>(); // ID -> status
+
+ /**
+ * A new copy of a book is bought and stored in the book manager.
+ * @param bc the new book copy.
+ */
+ public void buy (BookCopy bc) {
+ updateView (bc);
+ }
+
+ /**
+ * Remove a book copy, e.g., after it was lost.
+ * @param bc
+ */
+ public void drop (BookCopy bc) {
+ view.remove(bc.getID());
+ }
+
+ /**
+ * Print some information on each book managed by us.
+ */
+ public void printView () {
+ System.out.println("Copy\t\t\tStatus");
+ System.out.println("--------------------------------");
+ String[] keys = view.keySet().toArray(new String[view.size()]);
+ Arrays.sort(keys); // sorting is for testability.
+ for (int i=0; i<keys.length; i++) {
+ String key = keys[i];
+ String status = view.get(key);
+ System.out.println(key + "\t" + status);
+ }
+ }
+
+ /**
+ * Update the status (available/out) of a given book copy.
+ * @param bc
+ */
+ public void updateView(BookCopy bc) {
+ String status = bc.isAvail() ? "available" : "out";
+ view.put(bc.getID(), status);
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Person.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Person.java
new file mode 100644
index 000000000..8c7ea806c
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/library/Person.java
@@ -0,0 +1,34 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.library;
+
+/**
+ * While Persons are actually quite complex, this class models only
+ * what is needed to identify a Person when borrowing a book copy.
+ */
+public class Person {
+
+ private String name;
+
+ public Person (String n) {
+ name = n;
+ }
+
+ public String getName () { return name; }
+
+ public String toString() { return name; }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/pattern/ObserverPattern.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/pattern/ObserverPattern.java
new file mode 100644
index 000000000..e52d260e1
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/pattern/ObserverPattern.java
@@ -0,0 +1,106 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.pattern;
+
+import java.util.LinkedList;
+
+/**
+ * This team gives a reusable implementation of the Observer Pattern.
+ * Only application-specific parts are left abstract.
+ *
+ * Two applications of this pattern and a simple Main can be found in package org.eclipse.objectteams.example.observer.application.
+ *
+ * To run the application, select org.eclipse.objectteams.example.observer.application/Main.java in the package explorer
+ * and select "Run as" -> "Java Application".
+ * (to check enablement of OT/J you may visit the JRE tab of the corres-
+ * ponding launch configuration and ensure that "Enable OTRE" is checked).
+ */
+public abstract team class ObserverPattern {
+
+ /**
+ * The Subject role of the observer pattern.
+ * Abstractness is not strictly needed, but it signals that applications
+ * of the pattern should refine this role.
+ */
+ protected abstract class Subject {
+
+ // ========== Registry of known Observers: ==========
+ private LinkedList<Observer> observers = new LinkedList<Observer>();
+
+ protected void addObserver (Observer o) {
+ observers.add(o);
+ }
+
+ protected void removeObserver (Observer o) {
+ observers.remove(o);
+ }
+
+ /**
+ * All edit operations of the concrete Subject should call into this method.
+ */
+ public void changeOp() {
+ for (Observer observer : observers)
+ observer.update(this);
+ }
+
+ /**
+ * Variant for multiple changes in one method call.
+ * Because we suspect reentrance at the base side, we temporarily deactivate this Team.
+ * (This solves the problem known as "jumping aspects"
+ * where more notifications would be sent than needed).
+ * By declaring the method as "callin" it is designed to be bound using "replace".
+ */
+ callin void changeOpMany () {
+ boolean wasActive = isActive();
+ deactivate(); // no notification during the base call.
+ base.changeOpMany(); // call original version (requires "callin" modifier).
+ if (wasActive)
+ activate(); // restore state
+
+ changeOp();
+ }
+ }
+
+ /**
+ * Observer role of the design pattern.
+ */
+ protected abstract class Observer {
+
+ /**
+ * This method needs to be realized to do something usefull
+ * on the actual observer instance.
+ */
+ protected abstract void update(Subject s);
+
+ /**
+ * To be called, when a concrete observer starts to participate in the pattern.
+ * @param s the subject to connect to.
+ */
+ public void start (Subject s) {
+ s.addObserver(this);
+ }
+
+ /**
+ * To be called, when a concrete observer stops to participate in the pattern.
+ * @param s the subject to disconnect from.
+ */
+ public void stop (Subject s) {
+ s.removeObserver(this);
+ }
+ }
+}
+
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Area.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Area.java
new file mode 100644
index 000000000..66fd5347a
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Area.java
@@ -0,0 +1,24 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.point_n_line;
+
+public class Area {
+
+ public void draw (Polyline l) {
+ System.out.println("Area drawing line "+l);
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Point.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Point.java
new file mode 100644
index 000000000..9aae5da9f
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Point.java
@@ -0,0 +1,28 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.point_n_line;
+
+public class Point {
+ private int x,y;
+ public Point (int x, int y) {
+ this.x = x;
+ this.y = y;
+ }
+ public String toString () {
+ return "("+x+", "+y+")";
+ }
+}
diff --git a/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Polyline.java b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Polyline.java
new file mode 100644
index 000000000..55cb648b2
--- /dev/null
+++ b/othersrc/otdt-examples/OTSample-Observer-src/src/org/eclipse/objectteams/example/observer/point_n_line/Polyline.java
@@ -0,0 +1,44 @@
+/**********************************************************************
+ * This file is part of "Object Teams Development Tooling"-Software
+ *
+ * Copyright 2004, 2010 Technical University Berlin, Germany.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * $Id$
+ *
+ * Please visit http://www.eclipse.org/objectteams for updates and contact.
+ *
+ * Contributors:
+ * Technical University Berlin - Initial API and implementation
+ **********************************************************************/
+package org.eclipse.objectteams.example.observer.point_n_line;
+
+import java.util.*;
+
+public class Polyline {
+ private LinkedList<Point> points = new LinkedList<Point>();
+
+ public void addPoint (Point p) {
+ points.add(p);
+ }
+
+ public void addPoints (List<Point> pList) {
+ for (Point p : pList)
+ addPoint(p);
+ }
+
+ public String toString () {
+ StringBuffer buf = new StringBuffer("Line [");
+ String sep = "";
+ for (Point p : points) {
+ buf.append(sep+p.toString());
+ sep = ", ";
+ }
+ buf.append("]");
+ return buf.toString();
+ }
+
+}

Back to the top