Skip to content

A missing pattern: Command, Query, Observer and ???

20130710 (2013 July 10 Wednesday)

When I read about the observer pattern in design patterns, I though but what about the other patterns. By this I mean the other patterns that govern the communication relationship between exactly 2 already existing objects.

There is a finite set of patterns governing the relationship between 2 objects that already exist. This finite number I believe is 4. (These relationships do not involve creation or destruction.) The 2 objects may have any combination of these relationships.

Notation

UML is inadequate for what is being shown here, so I have used another notation.

Rectangular box

A rectangular box represents an object

an arrow with a solid line

An arrow with a solid line, represents data flow

an arrow with a dotted line

An arrow with a dotted line represents control flow.

an arrow with a zigzag line

An arrow with a zigzag line represents synchronization, when to do something. A simple method call with possibly no arguments and no return value, and no action except to tell the called object that something has happened. The called object then synchronously does something.

The patterns — but only an introduction

Command

Also called: procedure.

Uses: method, routine or sub-routine
Used by: Command Query Separation
Implements: Setter, Do-er

Command methods/sub-routines should be verb phrases.

A Command is a method/sub-routine or even a code fragment, used to make an object do something. A command can be passed data, but can not retrieve(return) data, in it self. The command read-line-from-file returns nothing, though it will change the state of the object, the object will store this line of text and any error condition.

A controls and passed data to B

Query

Also called: function

Uses: method, routine or sub-routine
Used by: Command Query Separation, functional programming
Implements: getter

Query methods/sub-routines should be noun phrases, unless the return type is Boolean, in this case it should be an adjective phrase. It should not start with the verb get.

A Query is a method/sub-routine or even a code fragment, used to find out something about an object. A command can retrieve data, it can except data, but it can not change the object.

A controls B, but B passes data to A

Observer

Uses: method, routine or sub-routine
Used by:
Implements: dependency inversion

Observer is used when B is in control, but A knows when things need doing.

B Queries A, but at A's request

Note: It is an anti-pattern, to use observer to update state in B when state in A changes. Use Query chaining for this. Use observer to update objects that you do not know if they are being queried, e.g. a display that is queried when a person looks at it. Or to trigger processing, that can not be deferred e.g. event logging.

The other pattern

Also called: to be decided

Uses: method, routine or sub-routine
Used by: see below
Implements: see below

So first we see from the diagrams that the following diagram has not yet been described.
B Commands A, but at A's request

When I looked at this diagram I realized I had seen it before. It us used in Graphical User Interface frameworks, when there are multiple overlapping windows. If a window is covered the windowing system may forget what is covered; When the covered window is exposed the windowing system can not on its own redisplay the missing region. It therefore sends an expose event to the owner of the uncovered window. This owner then sends commands the the windowing system to redraw the area.

Used by: Windowing system, …
Implements: Refresh, …

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: