61
Design Patterns Steven R. Bagley

Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Design PatternsSteven R. Bagley

Page 2: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Last Time

• Introduced the Strategy Pattern

• Saw how it could solve our aquatic problems…

• Two design principles

Page 3: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Design Principle

“Identify the aspects of your application that vary and separate them from what stays the same”

Page 4: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Design Principle

“Program to an interface, not an implementation”

Page 5: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Today

• Consider the Strategy Pattern in more depth

• Design Patterns, the big picture

Page 6: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Mallard Redhead …

SwimQuackDisplay

PositionDuck

DisplayUpdateDucks

DucksDuckPond

*

Worked fine until we tried to make them flywhen our rubber ducks started to flyLots of overriding methods to hide implementation

Page 7: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”
Page 8: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Strategy Pattern

• Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable.

• Strategy lets the algorithm vary independently from clients that use it

• FlyBehaviour, QuackBehaviour

• Loosely coupled

Page 9: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

class CDuck{public:void PerformQuack();virtual void Swim();virtual void Display() = 0;void PerformFly();

protected:FlyBehaviour *m_flyBehaviour;QuackBehaviour *m_quackBehaviour;};

Page 10: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Change in Strategy

Page 11: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Change in Strategy

I wish I could fly!

Page 12: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Change in Strategy

I wish I could fly!

You can!

Page 13: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Orville

• Orville is a Duck

• That can’t fly

• Sings, not quack

• Can easily implement him

• Need a new behaviourQuacksWithIrritatingSong

Page 14: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

#include "Duck.h"

class CMallard : public CDuck{public:CMallard();virtual void Display();

};

Page 15: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

#include "Duck.h"

class COrville : public CDuck{public:COrville();virtual void Display();

};

Page 16: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

CMallard::CMallard(){m_flyBehaviour = new CFlyWithWings;m_quackBehaviour = new CQuack();}

void CMallard::Display(){printf("I'm a real Mallard duck\n");}

Page 17: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville::COrville(){m_flyBehaviour = new CFlyNoWay;m_quackBehaviour = new CQuacksSillySong();}

void COrville::Display(){printf("I'm Orville…\n");}

Page 18: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Orville

• Representation of new ducks is trivial

• Subclass Duck

• Add relevant behaviours

• Change Behaviour at runtime

Page 19: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Behaviour Modification

• Really Simple

• Mutators on Duck class

• flyBehaviour replaced with new instance

• Client can use Mutator to change behaviour

Page 20: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

class CDuck{public:void PerformQuack();void PerformFly();void SetFly(FlyBehaviour *fb);void SetQuack(QuackBehaviour *qb);

virtual void Swim();virtual void Display() = 0;

protected:FlyBehaviour *m_flyBehaviour;QuackBehaviour *m_quackBehaviour;};

Page 21: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

void CDuck::SetFly(FlyBehaviour *fb){if(fb){if(m_flyBehaviour)

delete m_flyBehaviour;

m_flyBehaviour = fb;}

}

Page 22: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

Page 23: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

Page 24: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyNoWay

m_quackBehaviour CQuacksSillySong

Page 25: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyNoWay

m_quackBehaviour CQuacksSillySong

Page 26: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyNoWay

m_quackBehaviour CQuacksSillySong

I'm Orville…

Page 27: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyNoWay

m_quackBehaviour CQuacksSillySong

I'm Orville…

Page 28: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyNoWay

m_quackBehaviour CQuacksSillySong

Can’t Fly

Page 29: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyNoWay

m_quackBehaviour CQuacksSillySong

Page 30: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyWithRocket

m_quackBehaviour CQuacksSillySong

Page 31: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyWithRocket

m_quackBehaviour CQuacksSillySong

Page 32: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

COrville *orville = new COrville();

orville->Display();orville->performFly();orville->SetFly(new CFlyWithRocket);orville->performFly();

m_flyBehaviour CFlyWithRocket

m_quackBehaviour CQuacksSillySong

Page 33: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Behaviour Modification

• Inheritance locks the behaviour into the class at compile-time

• Cannot be changed

• Except by copying the contents of the object into a new one of a different class

• Strategy Pattern (using composition) can be changed at runtime

Page 34: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Design Principle

“Favour composition over inheritance”

Page 35: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Has-a better than is-a

• Has-a relationship means it can be changed, is-a means fixed

• Many Design Patterns use composition over inheritance

• Our behaviours can be used elsewhere

Page 36: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Intrinsic/Extrinsic

• Use Inheritance for intrinsic properties

• Use Composition for extrinsic properties

Page 37: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Strategic Composition

• Our objects can be set to any behaviour

• Default behaviour, that can be overridden

• Provided it implements the correct interface

• Even if they are implemented later

• Supports Change

Page 38: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Strategy Pattern

• Duck behaviours are very simple

• Do not modify the Duck Object

• Some algorithms may need to work on the data inside the object

• Won’t this break encapsulation?

Page 39: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Line Breaking

• Formatting blocks of text

• Text stores as a long run of characters

• Various algorithms break it into lines

• Line-breaking algorithm may vary

Page 40: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Jackdaws love my big sphinx of quartz

Page 41: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Jackdaws love my big sphinx of quartz

Page 42: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Jackdaws love my big sphinxof quartz

Page 43: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Jackdaws lovemy big sphinxof quartz

Page 44: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Jackdaws lovemy big sphinxof quartz

Page 45: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Line-breaking Strategy

• Classic Case for Strategy Pattern

• Can’t vary algorithm with inheritance

• Separate TextBox from formatting algorithm

• Formatting goes in a LineBreaker object

• But TextBox contains the lines after formatting…

Page 46: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Formatter

Text

Lines

TextBox

Format()

LineBreaker

JustifiedLineBreaker CentredLineBreaker

TextLine * 1

Page 47: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Break Encapsulation

• Line breaking algorithm needs to modify internals of TextBox

• How do we do this?

• Make the data public?

• NOOOOOOOOO!!!!!!!!

Page 48: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Make Friends?

• What about making classes be friends?

• A class ‘friend’ is a concept that enables one class to specify that another class can modify its internals

• Supported in C++ and C# but not Java

• Better than public data

Page 49: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Break Friends

• Friends are not a good idea

• Line-breaking objects would be tied to working on TextBox only

• If TextBox changes, Line Breakers must be changed

• Tight coupling

Page 50: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Design Principle

“Program to an interface, not an implementation”

Page 51: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Line-break interface

• Separating the algorithm from the data

• Loose coupling

• LineBreaker should be distinct from TextBox (might want to use it elsewhere)

• Define an interface that LineBreaker can use to modify TextBox

Page 52: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

LineBreaker Interface

• What does LineBreaker need to do?

• Get the text to format?

• Break into lines

• Pass the lines of text back to the TextBox object

• This could be an interface implemented by TextBox

Page 53: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Fitting it together

• TextBox has-a LineBreaker

• TextBox calls format() on LineBreaker

• format takes an interface as a parameter

• LineBreaker uses interface to access text

• LineBreaker unaware of TextBox

Page 54: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

What interface?

• TextBox implements the interface LineBreaker uses

• TextBox has full control on how it uses the output of the algorithm

• TextBox still responsible for internal data, encapsulation intact (phew!)

Page 55: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Formatter

Text

Lines

TextBox

Format()

LineBreaker

JustifiedLineBreaker CentredLineBreaker

TextLine * 1

LineBreakerOutput

Page 56: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Using Patterns

• Design Patterns are for us, not the computer

• Start to think about problems at the pattern level, not the object level

• But don’t get pattern fever (patterns are not good for Hello World)

• Shared vocabulary for a development team

Patterns communicate a lot of information to other developers, quickly and simply in less words. The other developer knows exactly the kind of design you intend but without you needing to outline every object’s job. Keeps you focused on the design, without getting bogged down in implementation details (think about the ducks, Strategy solved problems that appeared in implementation)

Page 57: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Patterns for the Brain

• Unlike Libraries/Frameworks, Patterns are for you

• Can’t build a class library since they are higher level

• Think about a bridge

• Each Bridge design is specific

• But the design follow the same pattern

Page 58: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Isn’t it just OO design?

• OO principles don’t guarantee a good design

• Hard to come up with a good design straightaway

• Patterns provide proven designs that we can reuse

• Gathered into catalogues

Again think of bridges

Page 59: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

Pattern not found

• But what happens if you can’t find a pattern that fits

• Use the design principles that underly the patterns

• Think about how the design might need to change in the future

Page 60: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

OO Basics

• Abstraction

• Encapsulation

• Polymorphism

• Inheritance

Page 61: Design Patterns - eprg.org · Design Principle “Identify the aspects of your application that vary and separate them from what stays the same”

OO Principles

• Encapsulate what varies

• Favour composition over inheritane

• Program to interfaces, not implementations