gang of four (gof) oo design patterns - david r. cheriton ... · pdf filegang of four (gof) oo...

44
WATERLOO CHERITON SCHOOL OF COMPUTER SCIENCE Gang of Four (GoF) Gang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11 th , 2011 IMPORTANT NOTICE TO STUDENTS These slides are NOT to be used as a replacement for student notes. These slides are sometimes vague and incomplete on purpose to spark class discussions

Upload: doannhi

Post on 06-Feb-2018

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Gang of Four (GoF)Gang of Four (GoF)OO Design Patterns

CS 446/646 ECE452May 11th, 2011

IMPORTANT NOTICE TO STUDENTS

These slides are NOT to be used as a replacement for student notes.These slides are sometimes vague and incomplete on purpose to spark class discussions

Page 2: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 2WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

MotivationObject Oriented Analysis (OOA)● domain problem designed as (domain) objects

– addresses the functional challenges– what a system does– provides guidance for implementation

Object Oriented Design (OOD)● domain problem solved as (implementation) objects

– addresses the implementation challenges– how a system realizes OOA

Page 3: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 3WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

MotivationHow can we improve OOD● identify common characteristics

– creation, structure, behaviour & interactions

● design patterns (design reuse)– generic blueprints (micro architecture)– language and implementation independent– two main catalogues

● GoF: Gang of Four (Gamma, Helm, Johnson, Vlissides, 1995)● POSA: Pattern Oriented Software Architecture (Buschmann, et

al.; Wiley, 1996)

Page 4: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 4WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

MotivationWhat is a Design Pattern● common solution to a reoccurring problem in design

Anatomy● name● problem/motivation● solution● consequences & tradeoffs● which ones are important for us?

Page 5: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 5WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design Patterns ClassificationGoF Design Patterns

Creational Structural Behavioral

classscope

objectscope

Page 6: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 6WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design Patterns ClassificationGoF Design Patterns

Creational Structural Behavioral

Factory Method

Abstract Factory

Builder

Prototype

Singleton

Adaptor - class

Bridge

Composite

Decorator

Facade

Adaptor-object

Flyweight

Proxy

Interpreter

Chain of responsibility

Command

Iterator

Mediator

Template Method

Memento

Observer

State

Strategy

Visitor

classscope

objectscope

Page 7: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 7WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design Patterns Classification“Purpose” based classification● creational:

– concerns with creation process of objects & classes● structural

– composition of classes & objects● behavioral

– characterizes interaction & responsibility of objects & classes

Page 8: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 8WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design Patterns Classification“Scope” based classification● decided if the pattern applies to mainly classes or objects

Two categories● class scope

– relationship between classes & subclasses– statically defined at run-time

● object scope– object relationships (what type?)– can be manipulated at runtime (so what?)

Page 9: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 9WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Design Patterns ClassificationCreational class● defers object creation to

sub-classes (factory method)

Structural class● inheritance to compose

classes (adapter)

Behavioral class● uses inheritance to

describe flow of control, algorithms (template)

Creational object● defers object creation to

other objects (abstract factory)

Structural object● deals with object

assembly (adapter)

Behavioral object● group of objects working

together to carry out a task (iterator)

Page 10: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 10WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

SingletonIntent● “ensure a class only has one instance, and provide a

global point of access to it.”

Construction

Page 11: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 11WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

public class Singleton { private static final Singleton INSTANCE = new Singleton(); // Private constructor prevents // instantiation from other classes private Singleton() {} public static Singleton getInstance() { return INSTANCE; } }

SingletonIntent● “ensure a class only has one instance, and provide a

global point of access to it.”

Construction

Page 12: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 12WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

SingletonAdvantages● controlled access to the class instance(s)

– can dictate who, and when a client can access● refinement of functionality

– via inheritance/subclass

Page 13: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 13WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

SingletonAdvantages● variable number of instances

– the getInstance() method needs modification– what else needs to change?

Page 14: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 14WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

SingletonA closer look at Singleton● reuse● separation of concerns● global presence● stateful vs. stateless● multiple instances● life cycle

Page 15: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 15WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookReuse● coupling

– results in tighter coupling– couples with the exact type of the singleton object– pass by reference to reduce coupling?

Page 16: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 16WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookReuse● coupling

– results in tighter coupling– couples with the exact type of the singleton object– pass by reference to reduce coupling?

public void doSomething(){ Worker worker = Worker.getInstance(); worker.perform();}

public void doSomething(Worker worker){ worker.perform();}

Page 17: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 17WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookReuse● inheritance

– easy to extend functionality in subclasses– not easy to override the object instance in subclasses

Page 18: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 18WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookSeparation of concerns● singleton class responsible for creation

– acts as a builder/factory● what if we were to separate the two concerns

– example● database connection as a singleton● system 1 uses a singleton to ensure only a single database

connection● system 2 needs to connection pool of 10 databases connections

Page 19: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 19WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookGlobal presence● provides a global access point to a service

– aren't global variables bad?– can be accessed from anywhere

● violation of layered access● not part of method signature

– dependency is not obvious – requires code inspection

● a large system may require many singletons– use a registry/repository

Page 20: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 20WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookStateful singleton● same as a global variable in principle

– aren't global variables bad?● access concerns

– synchronization– concurrency – multiple threaded using a singleton

● mutable vs. immutable state

Stateless singleton● better then stateful● can we have a stateless singleton?

Page 21: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 21WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookMultiple instances● distributed systems

– is it possible to have a true singleton in a distributed system?

– global registries/repositories● language (Java) specific concerns

– initialization – has to be thread safe– serialization– class loaders

Page 22: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 22WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Singleton – A Closer LookLife-cycle & life span● creation

– lazy initialization● singletons are long lived

– as long as an application's life span– registries can outlive applications– unit testing requires short lived state

● language (Java) specific concern– reloading singleton class (servlets)– loss of state

Page 23: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 23WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

SingletonWhen can I use a singleton● considerations[1]

– will every user use this class exactly the same way?– will every applications ever need only one instance?– should the clients be unaware of the application

● examples– Java Math class (stateless – static class)– top level GUI (window/frame)– logging

[1] http://www.ibm.com/developerworks/library/co-single.html

Page 24: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 24WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

AdapterIntent● “convert the interface of a class into another interface...

Adapter lets classes work together that couldn't otherwise because of incompatible interface”

● also known as “wrapper”● boolean values can be represented by

– {1,0}, {true, false}, {yes, no}– does this qualify as an adapter?

Page 25: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 25WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Motivation

Need to add “Text” capability to our drawing editor.

Consider an off the shelf TextView component

Page 26: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 26WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Motivation

Observations● can be done in two ways

– object composition (shown above)– inheritance

● Shape provides “interface” and TextView provides an implementation

● Lets try to draw this?

Page 27: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 27WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter – ClassRequirement● requires multiple inheritance

what about implementations that do not support multipleinheritance (Java)?

Page 28: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 28WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter – ObjectRequirement● via object composition

Page 29: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 29WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter – Class vs. ObjectClass● commitment to a

concrete adaptee class– not to its subclasses

(class hierarchy)● allows for specialization

– how?● static in nature

Object● can use many adaptees

– including sub-classes● harder to override the

adaptee behavior– why?

Page 30: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 30WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter – Class vs. Object

Page 31: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 31WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter & Dependency Inversion

Client layer

Business layer

Utility layer

Client layer

Business layer

Utility Interface

Business Interface

Utility layer

Simple Layers Abstract Layers

Dependency Inversion (DI)● decouple high level layer from lower level layer(s)

Page 32: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 32WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Dependency Inversion ExampleImplications● Button implementation

relies on Lamp● any changes to Lamp will

impact Button● what if we want to reuse

Button class with a different component– such as a motor

Page 33: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 33WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Dependency Inversion ExampleDependency Inversion to Rescue● looks good (?)● still a dependency left

Page 34: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 34WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Dependency Inversion ExampleObservation● adapter enhanced the design

– increased re-usability at the price of complexity

Page 35: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 35WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

AdapterHow much adaptation is reasonable?

Page 36: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 36WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

BridgeIntent● “decouples an abstraction from its implementation so the

two can vary independently”

● does this not sounds like an adapter?– will take a closer look later

Page 37: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 37WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Bridge

Page 38: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 38WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Bridge

Bridge

Page 39: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 39WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Bridge Example

Solution via inheritance

problem1: what if we have to support another platform?

problem2: client code is tied to an implementation. For portable code, the client should not refer to an implementation

Problem

Page 40: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 40WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Bridge ExampleSolution: Use bridge pattern to place abstraction and implementation in two different hierarchies

Page 41: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 41WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Bridge ExampleSolution: Use bridge pattern to place abstraction and implementation in two different hierarchies

Bridge

Page 42: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 42WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

BridgeFeatures● flexible binding between abstraction & implementation● two class hierarchies● clients are decoupled

Page 43: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 43WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter & BridgeCommon Elements● flexibility via indirection● request forwarding

Page 44: Gang of Four (GoF) OO Design Patterns - David R. Cheriton ... · PDF fileGang of Four (GoF) OO Design Patterns CS 446/646 ECE452 May 11th, 2011 IMPORTANT NOTICE TO STUDENTS ... language

CS446/646 ECE452 44WATERLOOCHERITON SCHOOL OFCOMPUTER SCIENCE

Adapter & BridgeDifference in intent● adapter

– resolves incompatibilities between two existing interfaces– two interfaces are independent and can evolve separately– coupling is unforeseen– adapts components after they have been designed

● bridge– connects an abstraction and its many implementations– evolution is in accordance with the base abstraction– coupling between the abstraction and the implementations are

known