Index: src/Makefile.am
===================================================================
--- src/Makefile.am	(revision 2ededc23ca840f65c2141748eec08536adbb27d6)
+++ src/Makefile.am	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
@@ -11,4 +11,7 @@
 ACTIONSHEADER = Actions/Action.hpp Actions/MethodAction.hpp Actions/ActionSequence.hpp Actions/MakroAction.hpp Actions/ErrorAction.hpp Actions/ActionRegistry.hpp
 
+PATTERNSOURCE = Patterns/Observer.cpp
+PATTERNHEADER = Patterns/Observer.hpp
+
 VIEWSOURCE = Views/View.cpp Views/StringView.cpp Views/MethodStringView.cpp Views/StreamStringView.cpp
 VIEWHEADER = Views/View.hpp Views/StringView.hpp Views/MethodStringView.hpp Views/StreamStringView.hpp
@@ -20,6 +23,7 @@
 UIHEADER = ${ACTIONSHEADER} ${VIEWHEADER} ${MENUHEADER} UIElements/UIFactory.hpp UIElements/TextUIFactory.hpp UIElements/MainWindow.hpp UIElements/TextWindow.hpp UIElements/Dialog.hpp UIElements/TextDialog.hpp
 
-SOURCE = ${ANALYSISSOURCE} ${ATOMSOURCE} ${UISOURCE} bond.cpp bondgraph.cpp boundary.cpp config.cpp element.cpp ellipsoid.cpp errorlogger.cpp graph.cpp helpers.cpp info.cpp leastsquaremin.cpp linkedcell.cpp log.cpp logger.cpp memoryusageobserver.cpp moleculelist.cpp molecule.cpp molecule_dynamics.cpp molecule_fragmentation.cpp molecule_geometry.cpp molecule_graph.cpp molecule_pointcloud.cpp parser.cpp periodentafel.cpp tesselation.cpp tesselationhelpers.cpp vector.cpp verbose.cpp menu.cpp
-HEADER = ${ANALYSISHEADER} ${ATOMHEADER} ${UIHEADER} bond.hpp bondgraph.hpp boundary.hpp config.hpp defs.hpp element.hpp ellipsoid.hpp errorlogger.hpp graph.hpp helpers.hpp info.hpp leastsquaremin.hpp linkedcell.hpp lists.hpp log.hpp logger.hpp memoryallocator.hpp memoryusageobserver.hpp molecule.hpp molecule_template.hpp parser.hpp periodentafel.hpp stackclass.hpp tesselation.hpp tesselationhelpers.hpp vector.hpp verbose.hpp menu.hpp
+
+SOURCE = ${ANALYSISSOURCE} ${ATOMSOURCE} ${PATTERNSOURCE} ${UISOURCE} bond.cpp bondgraph.cpp boundary.cpp config.cpp element.cpp ellipsoid.cpp errorlogger.cpp graph.cpp helpers.cpp info.cpp leastsquaremin.cpp linkedcell.cpp log.cpp logger.cpp memoryusageobserver.cpp moleculelist.cpp molecule.cpp molecule_dynamics.cpp molecule_fragmentation.cpp molecule_geometry.cpp molecule_graph.cpp molecule_pointcloud.cpp parser.cpp periodentafel.cpp tesselation.cpp tesselationhelpers.cpp vector.cpp verbose.cpp menu.cpp
+HEADER = ${ANALYSISHEADER} ${ATOMHEADER} ${PATTERNHEADER} ${UIHEADER} bond.hpp bondgraph.hpp boundary.hpp config.hpp defs.hpp element.hpp ellipsoid.hpp errorlogger.hpp graph.hpp helpers.hpp info.hpp leastsquaremin.hpp linkedcell.hpp lists.hpp log.hpp logger.hpp memoryallocator.hpp memoryusageobserver.hpp molecule.hpp molecule_template.hpp parser.hpp periodentafel.hpp stackclass.hpp tesselation.hpp tesselationhelpers.hpp vector.hpp verbose.hpp menu.hpp
 
 BOOST_LIB = $(BOOST_LDFLAGS) $(BOOST_MPL_LIB)
Index: src/Patterns/Observer.cpp
===================================================================
--- src/Patterns/Observer.cpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
+++ src/Patterns/Observer.cpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
@@ -0,0 +1,142 @@
+/*
+ * Observer.cpp
+ *
+ *  Created on: Jan 19, 2010
+ *      Author: crueger
+ */
+
+#include "Observer.hpp"
+
+
+#include <iostream>
+
+using namespace std;
+
+/****************** Static stuff for the observer mechanism ************/
+
+// All infrastructure for the observer-pattern is bundled at a central place
+// this is more efficient if many objects can be observed (inherit from observable)
+// but only few are actually coupled with observers. E.g. TMV has over 500.000 Atoms,
+// which might become observable. Handling Observerable infrastructure in each of
+// these would use memory for each atom. By handling Observer-infrastructure
+// here we only need memory for objects that actually are observed.
+// See [Gamma et al, 1995] p. 297
+
+map<Observable*, int> Observable::depth;
+multimap<Observable*,Observer*> Observable::callTable;
+set<Observable*> Observable::busyObservables;
+
+// The two functions start_observer_internal and finish_observer_internal
+// have to be used together at all time. Never use these functions directly
+// START_OBSERVER and FINISH_OBSERVER also construct a bogus while(0) loop
+// thus producing compiler-errors whenever only one is used
+
+void Observable::start_observer_internal(Observable *publisher){
+  // increase the count for this observable by one
+  // if no entry for this observable is found, an new one is created
+  // by the STL and initialized to 0 (see STL documentation)
+  depth[publisher]++;
+}
+
+void Observable::finish_observer_internal(Observable *publisher){
+  // decrease the count for this observable
+  // if zero is reached all observed blocks are done and we can
+  // start to notify our observers
+  if(--(depth[publisher])){}
+  else{
+    publisher->notifyAll();
+    // this item is done, so we don't have to keep the count with us
+    // save some memory by erasing it
+    depth.erase(publisher);
+  }
+}
+
+/************* Notification mechanism for observables **************/
+
+typedef multimap<Observable*,Observer*>::iterator callIterator;
+typedef pair<callIterator, callIterator> iteratorRange;
+
+void Observable::notifyAll() {
+  // we are busy notifying others right now
+  // add ourselves to the list of busy subjects to enable circle detection
+  busyObservables.insert(this);
+  // see if anyone has signed up for observation
+  // and call all observers
+  if(callTable.count(this)) {
+     iteratorRange callerRange = callTable.equal_range(this);
+     callIterator iter;
+     for(iter=callerRange.first;iter!=callerRange.second;iter++){
+       (*iter).second->update(this);
+     }
+  }
+  // done with notification, we can leave the set of busy subjects
+  busyObservables.erase(this);
+}
+
+// this handles passing on updates from sub-Observables
+void Observable::update(Observable *publisher) {
+  // circle detection
+  if(busyObservables.find(this)!=busyObservables.end()) {
+    // somehow a circle was introduced... we were busy notifying our
+    // observers, but still we are called by one of our sub-Observables
+    // we cannot be sure observation will still work at this point
+    cerr << "Circle detected in observation-graph." << endl;
+    cerr << "Observation-graph always needs to be a DAG to work correctly!" << endl;
+    cerr << "Please check your observation code and fix this!" << endl;
+    return;
+  }
+  else {
+    // see if we are in the process of changing ourselves
+    // if we are changing ourselves at the same time our sub-observables change
+    // we do not need to publish all the changes at each time we are called
+    if(depth.find(this)==depth.end()) {
+      notifyAll();
+    }
+  }
+}
+
+// methods to sign-on and off
+void Observable::signOn(Observer *target) {
+  bool res = false;
+  iteratorRange callerRange = callTable.equal_range(this);
+  callIterator iter;
+  for(iter=callerRange.first;iter!=callerRange.second;iter++){
+    res |= ((*iter).second == target);
+  }
+  if(!res)
+    callTable.insert(pair<Observable*,Observer*>(this,target));
+}
+
+void Observable::signOff(Observer *target) {
+  iteratorRange callerRange = callTable.equal_range(this);
+  callIterator iter;
+  for(iter=callerRange.first;iter!=callerRange.second;iter++) {
+    if((*iter).second == target)
+      callTable.erase(iter);
+  }
+}
+
+// when an sub-observerable dies we usually don't need to do anything
+void Observable::subjectKilled(Observable *publisher){
+}
+
+Observable::Observable()
+{}
+
+// when an observable is deleted, we let all our observers know
+Observable::~Observable()
+{
+  // delete all entries for this observable
+  iteratorRange callerRange = callTable.equal_range(this);
+  callIterator iter;
+  for(iter=callerRange.first;iter!=callerRange.second;iter++){
+    (*iter).second->subjectKilled(this);
+  }
+  callTable.erase(callerRange.first,callerRange.second);
+}
+
+Observer::Observer()
+{}
+
+Observer::~Observer()
+{}
Index: src/Patterns/Observer.hpp
===================================================================
--- src/Patterns/Observer.hpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
+++ src/Patterns/Observer.hpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
@@ -0,0 +1,78 @@
+/*
+ * Observer.hpp
+ *
+ *  Created on: Jan 19, 2010
+ *      Author: crueger
+ */
+
+#ifndef OBSERVER_HPP_
+#define OBSERVER_HPP_
+
+#include <map>
+#include <set>
+
+/**
+ * Basic structure for the observer pattern
+ *
+ * Observers register themselves with the observables to be notified when something changes.
+ * In the Observable code that changes attributes should be wrapped in
+ * START_OBSERVER ... END_OBSERVER blocks. If other methods are called that also contain
+ * Observation blocks from there, these functions wont trigger notifications. Only the end
+ * of the final observation block triggers the update of Observers.
+ *
+ * Each observerable can have sub-observables. When one of these sub-observables changes and
+ * notifies its observers the observable that contains them will also notify its observers.
+ * This passing on of updates is blocked, when the main-observable is in the process of
+ * updating many of its internal sub-observables. This means the update is not passed, if
+ * it is produced while the main-observable itself is within any Observation block.
+ */
+
+class Observable;
+
+class Observer
+{
+public:
+  Observer();
+  virtual ~Observer();
+
+  virtual void update(Observable *publisher)=0;
+  virtual void subjectKilled(Observable *publisher)=0;
+};
+
+class Observable : public Observer {
+public:
+  Observable();
+  virtual ~Observable();
+
+  virtual void signOn(Observer *target);
+  virtual void signOff(Observer *target);
+
+  virtual void update(Observable *publisher);
+  virtual void subjectKilled(Observable *publisher);
+
+private:
+  virtual void notifyAll();
+protected:
+// Observer mechanismen is done from a static central place
+  /**
+   * Internal method.
+   * Do not call directly. Use START_OBSERVER macro instead
+   */
+  static void start_observer_internal(Observable *publisher);
+  /**
+   * Internal method.
+   * Do not call directly. Use FINISH_OBSERVER macro instead
+   */
+  static void finish_observer_internal(Observable *publisher);
+
+private:
+  static std::map<Observable*, int> depth;
+  static std::multimap<Observable*,Observer*> callTable;
+  static std::set<Observable*> busyObservables;
+};
+
+
+
+#define START_OBSERVER Observable::start_observer_internal(this);do{do{}while(0)
+#define FINISH_OBSERVER }while(0);Observable::finish_observer_internal(this)
+#endif /* OBSERVER_HPP_ */
Index: src/unittests/Makefile.am
===================================================================
--- src/unittests/Makefile.am	(revision 2ededc23ca840f65c2141748eec08536adbb27d6)
+++ src/unittests/Makefile.am	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
@@ -26,9 +26,11 @@
   Tesselation_BoundaryTriangleUnitTest \
   Tesselation_InOutsideUnitTest \
-  VectorUnitTest
+  VectorUnitTest \
+  ObserverTest \
+  ${MENUTESTS}  
     
   
-check_PROGRAMS = $(TESTS) ${MENUTESTS}
-noinst_PROGRAMS = $(TESTS) ${MENUTESTS}
+check_PROGRAMS = $(TESTS) 
+noinst_PROGRAMS = $(TESTS)
 
 ActOnAllUnitTest_SOURCES = ../test/ActOnAllTest.hpp ActOnAllUnitTest.cpp ActOnAllUnitTest.hpp
@@ -95,4 +97,7 @@
 ActionSequenceTest_LDADD = ../libmolecuilder.a
 
+ObserverTest_SOURCES = ObserverTest.cpp ObserverTest.hpp
+ObserverTest_LDADD = ../libmolecuilder.a
+
 #AUTOMAKE_OPTIONS = parallel-tests
 
Index: src/unittests/ObserverTest.cpp
===================================================================
--- src/unittests/ObserverTest.cpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
+++ src/unittests/ObserverTest.cpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
@@ -0,0 +1,185 @@
+/*
+ * ObserverTest.cpp
+ *
+ *  Created on: Jan 19, 2010
+ *      Author: crueger
+ */
+
+#include "ObserverTest.hpp"
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+
+#include "Patterns/Observer.hpp"
+
+#include <iostream>
+
+using namespace std;
+
+// Registers the fixture into the 'registry'
+CPPUNIT_TEST_SUITE_REGISTRATION( ObserverTest );
+
+/******************* Test stubs ************************/
+
+class UpdateCountObserver : public Observer {
+public:
+  UpdateCountObserver() :
+    updates(0)
+  {};
+  void update(Observable *publisher){
+    updates++;
+  }
+  void subjectKilled(Observable *publisher) {
+  }
+  int updates;
+};
+
+class SimpleObservable : public Observable {
+public:
+  void changeMethod() {
+    START_OBSERVER;
+    int i;
+    i++;
+    FINISH_OBSERVER;
+  }
+};
+
+class CallObservable : public Observable {
+public:
+  void changeMethod1() {
+    START_OBSERVER;
+    int i;
+    i++;
+    FINISH_OBSERVER;
+  }
+
+  void changeMethod2() {
+    START_OBSERVER;
+    int i;
+    i++;
+    changeMethod1();
+    FINISH_OBSERVER;
+  }
+};
+
+class SuperObservable : public Observable {
+public:
+  SuperObservable(){
+    subObservable = new SimpleObservable();
+    subObservable->signOn(this);
+  }
+  ~SuperObservable(){
+    delete subObservable;
+  }
+  void changeMethod() {
+    START_OBSERVER;
+    int i;
+    i++;
+    subObservable->changeMethod();
+    FINISH_OBSERVER;
+  }
+  SimpleObservable *subObservable;
+};
+
+/******************* actuall tests ***************/
+
+void ObserverTest::setUp() {
+  simpleObservable = new  SimpleObservable();
+  callObservable = new CallObservable();
+  superObservable = new SuperObservable();
+
+  observer1 = new UpdateCountObserver();
+  observer2 = new UpdateCountObserver();
+  observer3 = new UpdateCountObserver();
+}
+
+void ObserverTest::tearDown() {
+  delete simpleObservable;
+  delete callObservable;
+  delete superObservable;
+
+  delete observer1;
+  delete observer2;
+  delete observer3;
+}
+
+void ObserverTest::doesUpdateTest()
+{
+  simpleObservable->signOn(observer1);
+  simpleObservable->signOn(observer2);
+  simpleObservable->signOn(observer3);
+
+  simpleObservable->changeMethod();
+  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
+  CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
+  CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
+
+  simpleObservable->signOff(observer3);
+
+  simpleObservable->changeMethod();
+  CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
+  CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
+  CPPUNIT_ASSERT_EQUAL( 1, observer3->updates );
+}
+
+
+void ObserverTest::doesBlockUpdateTest() {
+  callObservable->signOn(observer1);
+
+  callObservable->changeMethod1();
+  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
+
+  callObservable->changeMethod2();
+  CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
+}
+
+void ObserverTest::doesSubObservableTest() {
+  superObservable->signOn(observer1);
+  superObservable->subObservable->signOn(observer2);
+
+  superObservable->subObservable->changeMethod();
+  CPPUNIT_ASSERT_EQUAL( 1, observer1->updates );
+  CPPUNIT_ASSERT_EQUAL( 1, observer2->updates );
+
+  superObservable->changeMethod();
+  CPPUNIT_ASSERT_EQUAL( 2, observer1->updates );
+  CPPUNIT_ASSERT_EQUAL( 2, observer2->updates );
+}
+
+
+void ObserverTest::CircleDetectionTest() {
+  cout << endl << "Warning: the next test involved methods that can produce infinite loops." << endl;
+  cout << "Errors in this methods can not be checked using the CPPUNIT_ASSERT Macros." << endl;
+  cout << "Instead tests are run on these methods to see if termination is assured" << endl << endl;
+  cout << "If this test does not complete in a few seconds, kill the test-suite and fix the Error in the circle detection mechanism" << endl;
+
+  cout << endl << endl << "The following error displayed by the observer framwork can be ignored" << endl;
+
+  // make this Observable its own subject. NEVER DO THIS IN ACTUAL CODE
+  simpleObservable->signOn(simpleObservable);
+  simpleObservable->changeMethod();
+  // when we reach this line, although we broke the DAG assumption the circle check works fine
+  CPPUNIT_ASSERT(true);
+}
+
+/********************************************** Main routine **************************************/
+
+int main(int argc, char **argv)
+{
+  // Get the top level suite from the registry
+  CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
+
+  // Adds the test to the list of test to run
+  CppUnit::TextUi::TestRunner runner;
+  runner.addTest( suite );
+
+  // Change the default outputter to a compiler error format outputter
+  runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
+                                                       std::cerr ) );
+  // Run the tests.
+  bool wasSucessful = runner.run();
+
+  // Return error code 1 if the one of test failed.
+  return wasSucessful ? 0 : 1;
+};
Index: src/unittests/ObserverTest.hpp
===================================================================
--- src/unittests/ObserverTest.hpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
+++ src/unittests/ObserverTest.hpp	(revision 63c1f67df77cc8176b6c0383d0c9e95cea19ac9f)
@@ -0,0 +1,48 @@
+/*
+ * ObserverTest.hpp
+ *
+ *  Created on: Jan 19, 2010
+ *      Author: crueger
+ */
+
+#ifndef OBSERVERTEST_HPP_
+#define OBSERVERTEST_HPP_
+
+#include <cppunit/extensions/HelperMacros.h>
+
+// forward declaration for test stubs
+class UpdateCountObserver;
+class SimpleObservable;
+class CallObservable;
+class SuperObservable;
+
+
+class ObserverTest :  public CppUnit::TestFixture
+{
+  CPPUNIT_TEST_SUITE( ObserverTest ) ;
+  CPPUNIT_TEST ( doesUpdateTest );
+  CPPUNIT_TEST ( doesBlockUpdateTest );
+  CPPUNIT_TEST ( doesSubObservableTest );
+  CPPUNIT_TEST ( CircleDetectionTest );
+  CPPUNIT_TEST_SUITE_END();
+
+public:
+  void setUp();
+  void tearDown();
+
+  void doesUpdateTest();
+  void doesBlockUpdateTest();
+  void doesSubObservableTest();
+  void CircleDetectionTest();
+
+private:
+  UpdateCountObserver *observer1;
+  UpdateCountObserver *observer2;
+  UpdateCountObserver *observer3;
+
+  SimpleObservable *simpleObservable;
+  CallObservable *callObservable;
+  SuperObservable *superObservable;
+};
+
+#endif /* OBSERVERTEST_HPP_ */
