March 2006 92.3913 Ron McFadyen 1
Design Patterns
In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem in software design.
A design pattern isn't a finished design that can be transformed directly into code; it is a description or template for how to solve a problem that can be used in many different situations.
March 2006 92.3913 Ron McFadyen 2
Design Patterns
Patterns originated as an architectural concept by Christopher Alexander. •1977: A Pattern Language: Towns, Buildings, Construction is a book on architecture - by Christopher Alexander, Sara Ishikawa and Murray Silverstein
In 1994: •Design Patterns: Elements of Reusable Object-Oriented Software -GoF•First Pattern Languages of Programs conference
Some online resourses:•en.wikipedia.org/wiki/Design_pattern_(computer_science)•www.dofactory.com
March 2006 92.3913 Ron McFadyen 3
Design Patterns
Design patterns are composed of several sections
Format used by the Gang of Four.
•Pattern Name and Classification•Intent•Also Known As•Motivation•Applicability•Structure•Participants
•Collaboration•Consequences•Implementation •Sample Code•Known Uses•Related Pattern
Of particular interest are the Structure, Participants, and Collaboration sections.
March 2006 92.3913 Ron McFadyen 4
Also known as publish subscribe
The essence of this pattern is that one or more objects (called observers or listeners) are registered (or register themselves) to observe an event which may be raised by the observed object (the subject). The object which may raise an event maintains a collection of the observers.
Observer Pattern
March 2006 92.3913 Ron McFadyen 5
Observer Pattern
The object that is responsible for the event is given the responsibility of monitoring for the event – this object is the subject.
Objects that are interested in the event must register with the subject as interested parties – as observers.
The subject will notify its observers when the event occurs.
Two interfaces are used: a subscriber interface and a subject interface.
March 2006 92.3913 Ron McFadyen 6
Observer Pattern
Observer: objects that want to be notified of a certain event. An observer must have an update method whereby it is notified of an event.
Subject: the object that triggers the event. It must implement:
attach (observer) - add an observer to its list of observers
detach (observer) - remove an observer from …
notify () - goes through its list of observers calling each observer’s update method
As needed - additional methods to allow an observer to get additional information
March 2006 92.3913 Ron McFadyen 7
Interfaces
«interface»
Subject
attach()
detach()
notify()
«interface»
Observer
update()
March 2006 92.3913 Ron McFadyen 8
Generic pattern
«interface»
Subject
attach()
detach()
notify()
«interface»
Observer
update()
ConcreteSubject
attach()
detach()
notify()
…
ConcreteObserver
update()
…
*
March 2006 92.3913 Ron McFadyen 9
Weather Station Example
«interface»
Subject
attach()
detach()
notify()
«interface»
Observer
update()
WeatherDataattach()detach()notify()
CurrentConditionsDisplayupdate()display()
*
StatisticsDisplayupdate()display()
ForecastDisplayupdate()display()
«interface»
DisplayElement
display()
March 2006 92.3913 Ron McFadyen 10
Weather Station Example
«interface»
Subject
attach()
detach()
notify()
«interface»
Observer
update()
WeatherDataattach()detach()notify()
CurrentConditionsDisplayupdate()display()
*
StatisticsDisplayupdate()display()
ForecastDisplayupdate()display()
«interface»
DisplayElement
display()
ObserverConcrete observer
Concrete subject
observersubject
Concrete observer
Concrete observer
March 2006 92.3913 Ron McFadyen 11
Object diagram
What collection of objects exist? How are they related?
March 2006 92.3913 Ron McFadyen 12
Behaviour
What is the typical behaviour expressed with sequence diagrams?
March 2006 92.3913 Ron McFadyen 13
Decorator pattern
This design pattern allows new/additional behavior to be added to an existing method of an object dynamically
Decorator works by wrapping a new "decorator" object around the original object, which is typically achieved by passing the original object as a parameter to the constructor of the decorator, with the decorator implementing the new functionality. The interface of the original object needs to be maintained by the decorator.
Decorators are alternatives to subclassing. Subclassing adds behaviour at compile time whereas decorators provide new behaviour at runtime.
March 2006 92.3913 Ron McFadyen 14
Decorator
1 componentcomponent
decoratorConcrete component
Concrete decoratorA
Concrete decoratorB
operation()
operation()
operation() operation()
operation()
Client
March 2006 92.3913 Ron McFadyen 15
Decorator textbook example
1 componentBeverage
Condiment DecoratorHouseBlend
cost()
getDescription
getDescription()DarkRoast
Decaf
cost()
cost()
cost()
Espressocost()
Milk Mocha Soy Whip cost()
getDescription()
cost()
getDescription()
cost()
getDescription()
cost()
getDescription()
March 2006 92.3913 Ron McFadyen 16
Decorator textbook example
Beverage
Condiment DecoratorHouseBlend DarkRoast
DecafEspresso
Milk Mocha Soy Whip
DecoratorDecorator
Concrete decorator
Concrete component
Component
Concrete decorator
Concrete decorator
Concrete decorator
Concrete component
Concrete component
Concrete component
March 2006 92.3913 Ron McFadyen 17
Object diagram
What collection of objects exist? How are they related?
March 2006 92.3913 Ron McFadyen 18
Behaviour
What is the typical behaviour expressed with sequence diagrams?
March 2006 92.3913 Ron McFadyen 19
Singleton pattern
Singleton is designed to restrict instantiation of a class to one (or a few) objects.
Useful when exactly one object is needed to coordinate actions across the system.
Singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist.
March 2006 92.3913 Ron McFadyen 20
Singleton pattern
If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected.
• eager instantiation• lazy instantiation - no resources until needed
The singleton pattern must be carefully constructed in multi-threaded applications.