Observer Pattern

Starting Point Analysis

In the Project Risk Analysis, I know that if I can develop a spreadsheet that can be access by a single user with multiple views, then I can use Pyro and my method to extend it to a distributed, multi-user spreadsheet. So I don't need to consider any network communication in my early phases of development. So developing a non-distributed, single-user spreadsheet is my present concern. This spreadsheet should support all the requirements except the distributed and multi-user requirement. Even for this simplified requirment, I use several phases for development.

Requirement in this phase

In this phase, the following requirment should be fulfilled:

Implement a spreadsheet class that store all cell values.

Implement a plot view class that can plot any part of the spreadsheet.

Implement a spreadsheet view class that can show any part of the spreadsheet.

Implement a text interface that allows user to select any parts of the spreadsheet for plot or spreadsheet viewing.

In this text interface, a user can change a cell's value (only numeric). When the spreadsheet is updated, all the view instance should also updated.

Observer Pattern Overview

The following simple description of observer pattern is from the book, Design Patterns. Please see page 293-299 of this book for more details.

Intent

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

Structure

Participants

Subject

knows its observers. Any number of Observer objects may observe a subject.

provides an interface for attaching and detaching Observer objects.

Observer

defines an updating interface for objects that should be notified of changes in a subject.

ConcreteSubject

stores state of interest to ConcreteObserver objects.

sends a notification to its observers when its state changes.

ConcreteObserver

maintains a reference to a ConcreteSubject object.

stores state that should stay consistent with the subject's.

implements the Observer updating interface to keep its state consistent with the subject's.

Collaborations

UML Design in this phase

In our project, we have a spreadsheet which should be observed by the different views. When the state of the spreadsheet is changed, all the views are updated. So, the observer pattern is quit suitable for this purpose. Based on this pattern, I design the first class diagram in UML, which is shown in the following diagram.

Steps in this phase

prototype 1

In this prototype, we build a simplest version. No Gui observer, no spreadsheet subject, no text interface for changing the state of subject. But the frame of observer pattern is there. It consists of:

subject class: containing only one single Data object.

plotobserver and spreadsheetobserver classes: simply print the changed value of data on the screen.

data class: implements the corresponding class in UML class diagram.

Since there is no interface for editing the subject. I simply use the python interactive interface to instantiate class objects and change the state of subject. The source code can be found here. The running result could look like this.

prototype 2

In prototype 1, the spreadsheet class is added so that a spreadsheetsubject instance can have a single spreadsheet instance. The spreadsheet class is also an aggregate of Data objects. With slight modification of spreadsheetsubject class, we can have our prototype 2. Run it as prototype 1, we can get the same result.

prototype 3

In this prototype, we build our own simple text-interface. To run source code, simple issue the command: python txtinterface.py. The running result would look like this.

prototype 4

In this prototype, we change the spreadsheetobserver into a real spreadsheeetobserver. In this real spreadsheetobserver, user can specify any part of the spreadsheet to plot as long as it is in the range of the spreadsheet which is referenced by the spreadsheetsubject. With a slight modification of txtinterface.py. the runing result of source code would be like this.

prototype 5

In this prototype, we change the plotobserver into a real plotobserver. In this real plotobserver, user can specify any one column, one row, or two consecutive column or rows for plotting. With a slight modification of txtinterface.py. the runing result of source code would be like this.

prototype 6

In this prototype, we build a more powerful text interface. As you may noticed in the source code, the setState() of spreadsheetsubject and spreadsheet classes are also changed to that stated in UML class diagram.