Combining design patterns

From CSSEMediaWiki
Revision as of 23:23, 8 August 2009 by Matthew Harward (Talk | contribs)
Jump to: navigation, search

Design patterns can be combined in numerous interesting ways to create super-patterns aka OO programs. A great example of this being done well is JUnit. The Gang of Four 1995 text also mentions the combination of patterns, with each chapter having a small section on related patterns.

Contents

Singleton + X

The singleton can be combined with a wide variety of patterns to create more specialised behaviour.

Proxy

By using a singleton on certain parts of a proxy a specific effect can be generated.

  • Real Subject - Adding a singleton here means that any number of proxys can access this one class. This creates a specialised behaviour where different proxies can access a central object in different ways without being concerned that the right object is being called.
  • Proxy - A singleton here makes sure that only a single proxy can be used to call any subject.
  • Subject - A simple singleton here breaks the proxy pattern as no subclasses are allowed. A complex singleton, i.e. one that allows a controlled number of instances could be very powerful.

Facade

This is often a logical approach as it makes obtaining the correct object to access the complex system underneath straightforward.

Patterns Using Inheritance

The power of using a singleton in the inheritance hierachy
  • Superclass - Out of all of the subclasses there may only be one instance.
  • Subclasses - Allows the inheritance hierarchy to have controlled constraints. See the attached image.

Note: the class vs object Encapsulation boundary becomes very important here.

Observer + X

Observer

  • While I suppose it would be technically possible for an object to observe itself, I can't think of a pratical purpose for this.
  • However, chaining observers together to add new information/functionality at each step is a possible technique.

Composite + X

Thoughts?

Personal tools