Design Patterns

A deign pattern is a general reusable solution to a commonly occurring problem in software design. They are often templates that are used by programmers or implemented in development frameworks.

However, in this and the following lesson, two of them are implemented in Parham.

  • Observer pattern
  • Decorator pattern
Observer Pattern

Observer pattern is a software design pattern in which an object called subject maintains a list of dependent objects, called observers and notifies them automatically of any state changes (wikipedia).

The observer pattern deals with two types of classes: Subject and Observer.

A Subject is an object whose state is monitored and an Observer is an object which is notified when the state of the Subject is changed.

For this purpose the Observer should have a method (notify) which is called whenever the state of the Subject changes.

Implementation

The observer pattern is implemented in the ObserverPattern component.

As it was discussed the component has two interface classes: Subject and Observer.

The Observer class has an extern method called notify which is called whenever the state of Subject changes. As an Observer may listen on more than one Subject, a reference to the Subject is passed in the notify method.

The Subject class has three methods: subscribe, unsubscribe and publish. A Subject calls subscribe whenever it needs to be notified whenever an Observer changes. unsubscribe is the opposite of subscribe. Finally, A Subject calls publish whenever its state changes.

In the implementation, every Subject has a linked list of Observers. Note that in the linked list a reference to the Observer should be stored. The reason is that it is possible that an Observer listens on an arbitrary number of Subjects.

For this reason, the component defines an internal class named PObserver. Then, it declares an aggregation declaration.

PObserver -> Observer observer;

It then introduces a linked list component by instantiating LinkedList.

LinkedList[Subject, PObserver];

Sample Usage

To demonstrate the use of ObserverPattern, it is used to implement a sample. In the sample there are a set of Sensors and a set of Displays. Whenever the value of a Sensor changes its Displays are notified using an instance of ObserverPattern.

The complete of the sample can be downloaded from the link provided at the beginning of this example. It is made from the following files.

  • ObserverPattern
  • LinkedList
  • SensorReader
  • App
  • I/O functions in C
Observer component

SensorReader[Sensor, Display]


ObserverPattern[Sensor, Display];


foreign void print

    (char[] format, char[] name, char[] label,

     double value);


Sensor

{

    char[] label;

    double value;

}


Sensor (char[] _label)

{

    label = _label;

    value = 0;

}


void Sensor.setValue (double _value)

{

    value = _value;

    publish ();

}


Display

{

    char[] name;

}


Display (char[] _name)

{

    name = _name;

}


void Display.notify (Sensor sensor)

{

    print (“display %s: New value of %s is: %fn”,

        name, sensor.label, sensor.value);

}


ObserverPattern[Subject, Observer]


extern void Observer.notify (Subject);


PObserver[];

PObserver -> Observer observer;


LinkedList[Subject, PObserver];


PObserver (Observer _observer)

{

   observer = _observer;

}


void Subject.subscribe (Observer o)

{

   insert (new PObserver (o));

}

void Subject.unsubscribe (Observer obs)

{

    PObserver o;


    for (o = head; o; o = o.next)

        if (o.observer == obs)

        {

            remove (o);

            return;

        }

}

void Subject.publish ()

{

   PObserver o;

   for (o = head; o; o = o.next)

       o.observer.notify (this);

}

SensorReader component