Observer

Intent

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Class diagram of the original GoF pattern

structure-GoF:observer

Improvement in MixJuice

Solved Problem(s)

Introduction problem
It is impossible to introduce Observer pattern into the existing program without modifying the source code. (This problem is not mentioned in the GoF book.)

Used programming technique(s)

Class extension for Subject.

Consequences

Structure and pseudo code

structure:observer_sample1

module original {
  define class Subject {
    define void sideEffectOperation() {...}
  }
}

module extension extends original {
  define abstract class Observer {
    define abstract void update();
  }

  class Subject {
    define void attach(Observer o) {...}
    define void detach(Observer o) {...}
    define void changed() {...}

    void sideEffectOperation() {
      original();
      changed();
    }
  }
}

Akira TANAKA <akr@m17n.org>, Yuuji ICHISUGI <y-ichisugi@aist.go.jp>

Top