« Back to Design Patterns in Object-Oriented Programming

INTRODUCTION


Contents:


Key reference: DPG4


The purpose of this document is to understand:

Definition and purpose of patterns

A pattern is an abstraction that describes a class of similar arrangements. An arrangement is a set of relationships (causal, logical, spatial or temporal) within which exist a set of elements of some kind. A design pattern is a pattern that addresses a class of similar purposes.

“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.” — Christopher Alexander (emphases added).

Levels of abstraction: What is a “design pattern” here?

What is or is not dealt with as a pattern depends on the chosen level of abstraction. At a higher level of abstraction, less abstract patterns may be dealt with as elements that are used in more abstract patterns. Hence, when talking about patterns, it is key to also talk about the level of abstraction in which they would be dealt as patterns and not elements.

Click here for an example.

For example, in software design, an array is a pattern, but being less abstract than normal level of abstraction in software design (i.e. writing modules, packages and applications), an array is dealt with more as an element than an arrangement of elements.

Hence, to be clear about our level of abstraction, here is an except from the book: “Design patterns are not about designs such as linked lists and hash tables that can be encoded in classes and reused as is. Nor are they complex, domain-specific designs for an entire application or subsystem. The design patterns in this book are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context. A design pattern names, abstracts, and identifies the key aspects of a common design structure that make it useful for creating a reusable object-oriented design.” (emphases added).

Essential elements of a design pattern

  1. Pattern name
    • Naming is the final step of mental integration
    • Lets us deal with the pattern at higher levels of abstraction
  2. Problem
    • Context and conditions leading to design challenges
    • It shows what and how a pattern addresses these challenges
  3. Solution
    • Conceptual solution to the above-mentioned problem
    • Lays the abstract basis of the pattern and its application
  4. Consequences
    • Costs and benefits of the pattern’s application
    • Tradeoffs and limitations of the pattern
    • Helps evaluate a pattern’s applicability in a use-case

Template for describing a design pattern

Why a template?


The template is as follows:

Pattern classification criteria

  1. Purpose
    • Creational
      Deal with object creation
    • Structural
      Deal with class/object composition
    • Behavioural
      Deal with class/object interation and responsibility distribution
  2. Scope
    • Class
      • Deal with class relationships
      • Established by inheritance
      • Static, i.e. fixed during compilation
    • Object
      • Deal with object relationships
      • Modifyable during runtime, hence more dynamic

The above two aspects combine as follows:

  ? Class Object
Creational Create .. via subclasses … via other objects
Structural Compose … via inheritance … via elements
Behavioural Organise … control-flow … cooperation

NOTE:


Things to note as you study further: