Merged Lecture Notes
Merged Lecture Notes
4 Memento Pattern
2.4.1 Identification
Name Classification Strategy
Memento Behavioural Delegation (Object)
Intent
“Without violating encapsulation, capture and externalise an object’s internal state
so that the object can be restored to this state later.” ([2]:283)
2.4.2 Structure
2.4.3 Problem
The memento pattern enables an object to be restored to its previous state. Memento can
be seen as a snapshot of the system at a particular point in time.
1
2.4.4 Participants
Originator
Memento
Caretaker
Command
Command can make use of mementos to maintain the state of commands, in the order they were issued, in order to support
undoable operations.
Iterator
Mementos can be used for iteration to maintain the state of the iterator.
Bridge
The bridge pattern can be applied in order to separate the interface from the implementation of the memento in order to
provide the wide interface between the originator and the memento without using the friend technique which violates
objectoriented encapsulation.
2
.3 Template Method Pattern
3.3.1 Identification
Name Classification Strategy
Template Method Behavioural Inheritance
Intent
Define the skeleton of an algorithm in an operation, deferring some steps to
subclasses. Template Method lets subclasses redefine certain steps of an algorithm
without changing the algorithm’s structure. ([2]:325)
3.3.2 Structure
The structure of the Template Method design pattern is given in Figure 2. The design
pattern comprises of two classes, the abstract class which defines the interface and the
template method function. This function defers implementation of primitive operations
to its subclass.
3.3.3 Problem
Two different components have significant similarities, but demonstrate no reuse of
common interface or implementation. If a change common to both components becomes
necessary, duplicate effort must be expended [3].
1
3.3.4 Participants
AbstractClass
ConcreteClass
Factory Method
Although the Factory Method is not a specialisation of the Template Method pattern,
it is related to the Template Method pattern. Many of the non-virtual methods that
participate as AnOperation() in solutions that apply the Factory Method pattern are
often template methods that, among others, call factory methods.
Adapter
Both the Adapter pattern and the Template Method pattern provides an interface
through which operations that are implemented in other classes are called. The
difference is that Adapter provides an interface to access non-complying operations
that cannot be changed while the operations accessed through the template method
is expected to comply and are most likely to change.
Builder
Both Builder and the Template Method pattern require a process to be encapsulated
in a method. In fact the method that has to be implemented by the concrete builders
to assemble a product is a template method.
2
4.3 Factory Method Pattern
4.3.1 Identification
Name Classification Strategy
Factory Method Creational Inheritance (Class)
Intent
Define an interface for creating an object, but let subclasses decide which class to
instantiate. Factory Method lets a class defer instantiation to subclasses. ([2]:107)
4.3.2 Structure
4.3.4 Participants
Creator
• declares the factory method which returns a product object
• default factory method implementations may return a default concrete product
ConcreteCreator
Product
ConcreteProduct
1
Figure 3: UML class diagram of ComplexNumber
Abstract Factory
The Factory Method may be used in the implementation of the Abstract Factory
design pattern.
Prototype
Factory Methods can be used to initialise prototypical objects. The prototype also
can be used instead of the factory method to avoid large parallel hierarchies.
Singleton
In only one instance of a concrete factory is required, the concrete factory can be
made a Singleton.
2
5.3 Prototype Pattern
5.3.1 Identification
Name Classification Strategy
Prototype Creational Delegation
Intent
Specify the kinds of objects to create using a prototypical instance, and create new
objects by copying this prototype (Gamma et al. [2]:117)
5.3.2 Structure
5.3.3 Problem
The constructor of a class contains computationally expensive or manually time
consuming procedures. These can be avoided by creating a copy of the object rather than
going through the entire creation process each time from the beginning.
5.3.4 Participants
Prototype
ConcretePrototype
1
• Creates a new object by asking a prototype to clone itself
ConcretePrototype
PrototypeManager
Client
There are many practical scenarios where this design pattern really helps. Here follows
few of them mentioned by Gibson [3]
2
Factory Method
Both Prototype and Factory Method are creational patterns. However, Prototype use
delegation to create while Factory Method uses inheritance. They can also be used
together. One can design a factory method that accept arguments and uses these
arguments to find the correct prototype object, calls clone() on that object, and
returns the result. The client replaces all references to the new operator with calls
to the factory method.
Abstract Factory
Prototype and Abstract Factory are competing patterns in some ways. Prototype
define new types simply by creating new prototypes while Abstract Factory requires
the creation of new classes for defining new types. However, they can also be used
together. An Abstract Factory might store a set of prototypes from which to clone
and return product objects.
Abstract Factory and Builder
Similar to the prototype design pattern, these patterns hides the concrete product
classes from the client, thereby reducing the number of names clients know about.
Composite and Decorator
Designs that make heavy use of the Composite and Decorator patterns often can
benefit from Prototype.
Singleton and Abstract Factory Prototype, Singleton and Abstract Factory are all
creational patterns where you don’t use the new keyword to create a product but
you call a method that will create the specific product and return a pointer to it.
Singleton, Memento and Flyweight
These patterns administrate access to specific object instances similar to how
Prototype administrates it. All of them offer factory methods to clients and share a
create-on-demand strategy [4].
3
7.2 Abstract Factory Pattern
7.2.1 Identification
Name Classification Strategy
Abstract Factory Creational Delegation (Object)
Intent
Provide an interface for creating families of related or dependent objects without
specifying the concrete classes. (Gamma et al. [1]:87)
7.2.2 Structure
7.2.3 Participants
AbstractFactory
• provides an interface to produce abstract product objects
ConcreteFactory
AbstractProduct
1
• provides an interface for product objects
ConcreteProduct
• implements the abstract operations that produce product objects that are
created by the corresponding ConcreteFactory
Client
Template Method
May be used within the abstract factory and product hierarchies.
Singleton
Concrete factories may be implemented as Singletons..
2
8.3 Strategy Design Pattern
8.3.1 Identification
Name Classification Strategy
Strategy Behavioural Delegation
Intent
Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients that
use it
([4]:315)
8.3.2 Structure
8.3.3 Problem
An implementation implementing different algorithms to solve the same problem requires
uncontrolled coupling with its clients that can be avoided. That is, the implementation
consists of a variation of classes for implementing different algorithms to solve a given
problem and clients need to couple individually with these classes.
8.3.4 Participants
Strategy
ConcreteStrategy
1
Context
2
9.3 State Design Pattern
9.3.1 Identification
Name Classification Strategy
State Behavioural Delegation
Intent
Allow an object to alter its behaviour when its internal state changes.The object will
appear to change its class. ([1]:305)
9.3.2 Structure
9.3.3 Problem
The two major problem areas [2] that exist in which the state pattern can make a
contribution are:
The behaviour an object exhibits is dependent on the state of the object. Changing the state
of an object will therefore influence the behaviour of the object at run-time. When objects
become large, changing their state can become difficult. In order to control the complexity
of changing state, the state of the object is managed externally to the object itself.
An object may be required to change state many times and into many different states.
When these states become numerous and the flow is controlled by choice-statements
(such as if or switch in C++), the ability to manage the statement flow diminishes. In order
to control this complexity, the state of an object can be managed externally to the object
itself by modelling the states as objects in their own right.
1
9.3.4 Participants
State
ConcreteState
Context
• Maintains an instance of a ConcreteState subclass that defines the current state.
• Defines the interface of interest to clients.
Singleton or Prototype
When implementing the state pattern, the programmer has to decide on how the
state objects will be created. Often the application of the Prototype pattern will be
ideal. State objects are also often Singletons.
Flyweight
State objects can be shared by applying Flyweight.
2
11.3 Composite Design Pattern
11.3.1 Identification
Name Classification Strategy
Composite Structural Delegation (Object)
Intent
Compose objects into tree structures to represent part-whole hierarchies. Composite
lets clients treat individual objects and compositions of objects uniformly. ([2]:163)
11.3.2 Structure
11.3.3 Problem
Used in hierarchies where some objects are composite of other. Makes use of a store for
the children defined by Composite
11.3.4 Participants
Component
1
• manipulates the objects that comprise the composite.
11.4 Composite Pattern Explained
The composite pattern inherently builds a tree (refer to Figure 2) with the intermediate
nodes being instances of composite and the leaf nodes being instances of the leaf
participants.
Decorator
Used in conjunction with components to add state to the components. When a
decorator and a composite are combined, they usually share the same parent class.
Flyweight
Allows sharing of objects, particularly the leaf nodes
2
12.2 Decorator Pattern
12.2.1 Identification
Name Classification Strategy
Decorator Structural Delegation (Object)
Intent
Attach additional responsibilities to an object dynamically. Decorators provide a
flexible alternative to subclassing for extending functionality. ([1]:175)
12.2.2 Structure
12.2.3 Participants
Component
• interface for objects that can have responsibilities dynamically added to them
ConcreteComponent
Decorator
ConcreteDecoratorA
ConcreteDecoratorB
1
• adds behavioural-based responsibilities to the component
2
14.2 Observer Pattern
14.2.1 Identification
Name Classification Strategy
Observer Behavioural Delegation (Object)
Intent
Define a one-to-many dependency between objects so that when one object changes
state, all its dependents are notified and updated automatically. ([1]:293)
14.2.2 Structure
14.2.3 Problem
The observer design pattern solves the problem of needing to change the code for every
observer that is related to the subject during maintainance when observers are changed,
added or removed from the system. The pattern facilitates the attaching and detaching of
the observers from the subject leaving the subjects code intact.
14.2.4 Participants
Subject
• Provides an interface for observers to attach and detach to the concrete subject.
ConcreteSubject
1
• Implements the functionality to store objects that are observing it and sends
update notifications to these objects.
Observer
ConcreteObserver
2
15.2 Iterator Design Pattern
15.2.1 Identification
Name Classification Strategy
Iterator Behavioural Delegation
Intent
Provide a way to access the elements of an aggregate object sequentially without
exposing its underlaying representation ([3]:257)
15.2.2 Problem
A system has wildly different data structures that are often traversed for similar results.
Consequently traversal code is duplicated but with minor differences because each
aggregate has its own way to provide the functionality to access and traverse its objects.
To eliminate such duplication, we need to abstract the traversal of these data structures
so that algorithms can be defined that are capable of interfacing with them transparently
[4].
15.2.3 Structure
1
15.2.4 Participants
Iterator
Concrete Iterator
Aggregate
Concrete Aggregate
2
Department of Computer Science
17.2.1 Identification
Name Classification Strategy
Mediator Behavioural Delegation
Intent
Define an object that encapsulates how a set of objects interact. Mediator promotes
loose coupling by keeping objects from referring to each other explicitly, and it lets
you vary their interaction independently. ([1]:273)
17.2.2 Structure
17.2.3 Participants
Mediator
ConcreteMediator
1
• each colleague communicates with its mediator whenever it would have
otherwise communicated with another colleague.
17.2.4 Problem
We want to design reusable components, but dependencies between the potentially
reusable pieces demonstrates the spaghetti code phenomenon. When one wants to reuse
only one or a few of the classes in a group of classes, it is virtually impossible to isolate
them because they are to interconnected with one another. Trying to scoop a single serving
results in an all or nothing clump [2].
2
18.2 Command Design Pattern
18.2.1 Identification
Name Classification Strategy
Command Behavioural Delegation
Intent
Encapsulate a request as an object, thereby letting you parameterise clients with
different requests, queue or log requests, and support undoable operations.
([1]:263)
18.2.2 Problem
Used to modify existing interfaces to make it work after it has been designed.
18.2.3 Structure
18.2.4 Participants
Command
ConcreteCommand
Invoker
1
Receiver
• knows how to perform the operations associated with carrying out a request.
Any class may serve as a Receiver.
2
19.2 Adapter Design Pattern
19.2.1 Identification
Name Classification Strategy
Adapter Structural Inheritance (Class) and
Delegation (Object)
Intent
Convert an interface of a class into another interface clients expect. Adapter lets
classes work together that couldn’t otherwise because of incompatible interfaces.
([2]:139)
19.2.2 Problem
Used to modify existing interfaces to make it work after it has been designed.
19.2.3 Structure
The Adapter design pattern is the only pattern to which one of two structures can be
applied. The pattern can either make use of delegation or inheritance to achieve its intent.
The delegation structure is referred to as an Object Adapter, Figure 1, and the inheritance
structure as shown in Figure 2 for the Class Adapter.
1
Figure 2: The structure of the Class Adapter Design Pattern
19.2.4 Participants
Adaptee
Target
Adapter
Client
2
22.2 Builder Design Pattern
22.2.1 Identification
Name Classification Strategy
Builder Creational Delegation
Intent
Separate the construction of a complex object from its representation so that the
same construction process can create different representations. ([1]:97)
22.2.2 Structure
22.2.3 Participants
Builder
Concrete Builder
1
Director
Product
22.2.4 Problem
An application maintains a complex aggregate and provide for the construction of different
representations of the aggregate. There is a need to design the application in such a way
that the addition of more representations of the aggregate would require minimal
modification of the application.
2
23.2 Interpreter Design Pattern
23.2.1 Identification
Name Classification Strategy
Interpreter Behavioural Inheritance
Intent
Given a language, define a represention for its grammar along with aninterpreter
that uses the representation to interpret sentences in thelanguage. ([2]:243)
23.2.2 Structure
23.2.3 Participants
AbstractExpression
1
NonterminalExpression
• one such class is required for every rule R ::= R1 R2 ... Rn in the grammar.
• maintains instance variables of type AbstractExpression for each of the
symbols R1 through Rn.
• implements an Interpret operation for nonterminal symbols in the grammar.
Interpret typically calls itself recursively on the variables representing R1
through Rn.
Context
Client
23.2.4 Problem
A class of problems occurs repeatedly in a well-defined and well-understood domain. If
the domain were characterized with a language that can be expressed in terms of a formal
grammar, then problems could be easily solved with an interpretation engine
representing the grammar. [3].
2
24.2 Bridge Design Pattern
24.2.1 Identification
Name Classification Strategy
Bridge Structural Delegation
Intent
Decouple an abstraction from its implementation so that the two can vary
independently ([2]:151)
24.2.2 Structure
24.2.3 Participants
Abstraction
Refined Abstraction
1
Implementor
• defines the interface for implementation classes. This interface doesn’t have to
correspond exactly to Abstraction’s interface; in fact the two interfaces can be
quite different. Typically the Implementor interface provides only primitive
operations, and Abstraction defines higher-level operations based on these
primitives.
Concrete Implementor
24.2.4 Problem
A system has a proliferation of classes resulting from a coupled interface and numerous
implementations [4].