[.] DejaVU Online: -- The OO Lectures
[.] - [up] [top] - [I] [II] [III] [IV] - [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] - [A] [R]

Design Patterns


Design Patterns


slide: Design Patterns

Creational Patterns


Factory

provide interface for creating family of related or dependent objects without specifying their concrete classes

Example

Also known as kit

Paricipants

Consequences

Implementation

See GOF
slide: Factory


Factory Method

define interface for creating an object, but let subclass decide which class to instantiate

Also know as

virtual constructor

Motivation

  • encapsulates knowledge of which subclass to instantiate

Applicability

  • when a class can not anticpate the class of the objects it creates
  • to delegate responsibility of object creation

Structure

Participants

  • Product, ConcreteProduct, Creator, ConcreteCreator

Consequences

  • provide hooks for subclasses
  • connects parallel class hierarchies

Related

  • Factory, Prototype

slide: Factory Method


Singleton

ensure a class has only one instance and provide a global point of access

Applicability

  • only one instance of a class
  • instance must be extendable by subclassing

Implementation

     // .h file 
  
     class Singleton {
     public:
  	static Singleton instance();
     protected: 
limit access

Singleton(); private: static Singleton* _instance; }; // .c file Singleton* Singleton::_instance = 0; Singleton* Singleton::instance() { if (_instance == 0) _instance = new Singleton(); return _instance; }

Consequences

  • coordinate access to a single instance
  • reduced namespace
  • permit refinement
  • permit variable number of instances

Etcetera

see GOF
slide: Singleton


Prototype

specify kinds of objects to create using a prototypical instance and create by copying this prototype

Motivation

  • flexibility, reduce number of classes

Applicability

Participants

  • Prototype, ConcretePrototype, Client

Consequences

  • adding and removing product types at runtime
  • specifying new objects by varying value/structure
  • reduced subclassing
  • configure an application with classes dynamically see kit.h and dv/tk/kit.c

Related

  • AbstractFactory versus Prototype

slide: Prototype

Structural Patterns


Structural Patterns

are concerned with how classes and objects are composed to form larger structures
PatternAliasRemarks


slide: Structural Patterns


Language mechanisms

  • inheritance -- class
  • delegation -- object

Adaptor versus Bridge

wrapper

  • Adaptor resolves inconsistencies between interfaces
  • Bridge relates abstraction to ... implementation

Composite vs Decorator vs Proxy

recursive composition

  • Composite represents structure
  • Decorator adds responsibilities
  • Proxy regulates access

slide: Idioms or Language mechanisms

Behavioral Patterns


Behavioral Patterns

communication

deal with algorithms and the assignment of responsibilities between objects

class

  • Template Method -- the skeleton of an algorithm
  • Interpreter -- to evaluate expressions

object

composition

  • Mediator -- provides indirection for loose coupling
  • Chain of responsibility -- connect objects to interact
  • Observer -- to handle dependencies

slide: Behavioral Patterns


Encapsulating behavior

objectify!

  • Strategy -- algorithm
  • Command -- request
  • State -- object state -> behavioral change
  • Visitor -- to extract behavior from class
  • Iterator -- access and traversal

slide: Encapsulating behavior


Observer

one-to-many dependencies and notification

Structure

Consequences

  • abstract coupling between subject and observer
  • support for broadcast
  • deals with unexpected updates

Implementation

  • mapping subjects to observers
  • observing more than one subject?
  • who triggers the update - subject or client of subject
  • dangling references to subject
  • consistency of subject (before notification)
  • adding observer-specific update protocols (push / pull)
  • specifying modifications of interest (aspects)
  • how to encapsulate update semantics

slide: Observer Pattern


[.] - [up] [top] - [I] [II] [III] [IV] - [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] - [A] [R]
Hush Online Technology
hush@cs.vu.nl
02/15/00