Behavioural software patterns

These patterns cover algorithms and the assignment of responsibilities between objects.

Iterator – this pattern is concerned with allowing iterative access to a composition without exposing the underlying implementation.

Typically an iterator is an abstract interface and the composite object creates a concrete iterator that implements it – but the iterator interface is what is used by other objects.

Mediator – the mediator pattern is concerned with ensuring loose coupling between objects by defining interfaces that allow them to interact without knowledge of each others’ internals.

Observer – this pattern defines a one-many dependency in which one object – called the subject – notifies subscribing objects of changes in its state.

UML to describe the Observer pattern
From Wikipedia

State – this pattern is about allowing an object to change its behaviour when its state changes. The object will appear to change its class.

The Gang of Four give the example of a TCP connection – which will respond differently to datagrams depending if it is in the LISTEN, ESTABLISHED or CLOSED state. In each of these cases a different subclass will handle behaviour, though a common interface is used by all three.

UML for state pattern
From Wikipedia

Strategy – this pattern allows objects with different algorithms to operate through a common interface. Typical examples in text books and lecture notes are including paper aeroplanes and rubber ducks in collections of aircraft and avians!

So, for instance, all aircraft may have a fly() method in their abstract interface, but the concrete implementation for the paper aeroplane implements behaviour appropriate for that type of craft.

UML for strategy pattern
From wikipedia

Template Method  – this pattern creates a skeleton for an algorithm but allows subclasses to over-ride specific parts.

Template method UML Class Diagram
Image via Wikipedia

Visitor – the visitor software pattern is concerned with allowing new algorithms to be applied to an object without changing the object itself. Instead a new visitor class implements the visitor interface. In this way many new methods can be dynamically added to a class.

Class Diagram for the Visitor Pattern
Image via Wikipedia

The “Gang of Four” software patterns

Cover of "Design Patterns: Elements of Re...
Cover via Amazon

Not about software development in the People’s Republic of China after the end of the Cultural Revolution: this “Gang of Four” are Erich Gamma, Richard Helm, Ralph Johnson and John M. Vlassides, the authors of what many (I have not read it) regard as a classic of computer books – Design patterns : elements of reusable object-oriented software.

Their book is partly a catalogue of design patterns and it is this aspect I am concerned with here.

They divided patterns into three types:

  • Creational: concerned with object instantiation
  • Structural: The composition and organisation of objects into larger structures
  • Behavioural: Algorithms, interactions and control flow between objects


GRASP software patterns

This is a UML diagram (created in IBM Rhapsody...
Image via Wikipedia

My final exam is on Tuesday and covers object-orientated design and programming – hence the Groovy Game of Life code.

Software patterns are a big part of the course and part of that is the simple task of remembering what each pattern does. I have found Head First Design Patterns useful but it covers few patterns and certainly not the basic GRASP ones, that I do have to know.

GRASP stands for General Responsibility Assignment Software Patterns (or Principles – I actually think this comes closer to expressing what this is all about anyway).

These are:

Information Expert, solving the problem of the general principle of assigning responsibilities to objects by assigning the responsibilities to the information expert – the class that has the information necessary to fulfil the responsibility.

Creator, solving the problem of what should be responsible for creating a new instance of some class by assigning the responsibility of creating a new instance of class A  to the class, B, that:

  • aggregates A objects; or
  • contains A objects; or
  • records instances of A objects; or
  • closely uses A objects; or
  • has the initialising data that will be passed to A when it is created.

(If more than one class displays these characteristics then aggregation or composition should be preferred.)

Low Coupling, solving the problem of creating classes that have low levels of dependency and face a low impact when other classes are changed and can be more easily reused by assigning responsibilities so that coupling remains low.

Controller, which addresses the question of what should be responsible for handling an input system event by assigning the responsibility to a class that represents one of the following choices:

  • It represents the overall system, device or subsystem (it is a facade controller); or
  • It represents a use case scenario within which the system event occurs, often named Handler or Coordinator, etc (it is a use-case or session controller).

High Cohesion, which addresses the problem of how to keep complexity manageable and how to keep objects focused by assigning responsibilities so that cohesion remains high.