Strategy: A Behavioral Design Pattern

19
Strategy: A Behavioral Design Pattern By Anne Ryan

description

Strategy: A Behavioral Design Pattern. By Anne Ryan. What is a Design Pattern?. A design pattern deals with interactions between individual software components They are recurring solutions to common problems of design!. Types of Design Patterns. Creational Structural Behavorial - PowerPoint PPT Presentation

Transcript of Strategy: A Behavioral Design Pattern

Page 1: Strategy: A Behavioral Design Pattern

Strategy:A Behavioral Design Pattern

By Anne Ryan

Page 2: Strategy: A Behavioral Design Pattern

What is a Design Pattern?

A design pattern deals with interactions between individual software components

They are recurring solutions to common problems of design!

Page 3: Strategy: A Behavioral Design Pattern

Types of Design Patterns

• Creational

• Structural

• Behavorial– Strategy is a behavioral design pattern

Page 4: Strategy: A Behavioral Design Pattern

Strategy

• Define a family of algorithms

• Encapsulate each one

• Make them interchangeable

In a Strategy design pattern, you will:

Page 5: Strategy: A Behavioral Design Pattern

You should use Strategy when:

• You have code with a lot of algorithms

• You want to use these algorithms at different times

• You have algorithm(s) that use data the client should not know about

Page 6: Strategy: A Behavioral Design Pattern

Strategy Class Diagram

Context

contextInterface()

Strategy

algorithmInterface()

ConcreteStrategyC

algorithmInterface()

ConcreteStrategyB

algorithmInterface()

ConcreteStrategyA

algorithmInterface()

Page 7: Strategy: A Behavioral Design Pattern

Strategy vs. Subclassing

• Strategy can be used in place of subclassing

• Strategy is more dynamic

• Multiple strategies can be mixed in any combination where subclassing would be difficult

Page 8: Strategy: A Behavioral Design Pattern

Subclassing

Class

functionX()

SubClass1

functionX()

SubClass2

functionX()

SubClass3

functionX()

Page 9: Strategy: A Behavioral Design Pattern

Add a function

Class

functionX()functionY()

SubClass1

functionX()

SubClass2

functionX()

SubClass3

functionX()

Add functionY()

Page 10: Strategy: A Behavioral Design Pattern

What happens? Class

functionX()functionY()

SubClass1

functionX()functionY()

SubClass2

functionX()functionY()

SubClass3

functionrX()functionY()

SubClass2.1

behaviorY()

SubClass2.2

behaviorY()

Need SIX classes to handle both functions!!!

Page 11: Strategy: A Behavioral Design Pattern

Strategy makes this easy!

Class

functionX()functionY()

StrategyX

functionX()

...StrategyY

functionY()

...

Page 12: Strategy: A Behavioral Design Pattern

Benefits of Strategy

• Eliminates conditional statements – Can be more efficient than case statements

• Choice of implementation– Client can choose among different

implementations with different space and time trade-offs

Page 13: Strategy: A Behavioral Design Pattern

Benefits of Strategy

• Families of related algorithms

• Alternative to subclassing– This lets you vary the algorithm dynamically,

which makes it easier to change and extend– You also avoid complex inheritance structures

Page 14: Strategy: A Behavioral Design Pattern

Drawbacks of Strategy

• Clients must be aware of different strategies– Clients must know how strategies differ so it

can select the appropriate one

• Communication overhead between strategy and context– Sometimes the context will create and

initialize parameters that are never used

Page 15: Strategy: A Behavioral Design Pattern

Drawbacks of Strategy

• Increased number of objects– if the algorithm differences are simple, the

extra classes add extra complexity

Page 16: Strategy: A Behavioral Design Pattern

Implementation Issues

Context

contextInterface()

Strategy

algorithmInterface()

ConcreteStrategyC

algorithmInterface()

ConcreteStrategyB

algorithmInterface()

ConcreteStrategyA

algorithmInterface()

Page 17: Strategy: A Behavioral Design Pattern

Implementation Issues

• Concrete Strategy needs efficient access to data

• Should you use a template?

• Should you make strategy objects optional?

Page 18: Strategy: A Behavioral Design Pattern

Thank you to

Provider of the PowerPoint graphs:

http://vik.ktu.lt/moduliai/

Page 19: Strategy: A Behavioral Design Pattern

What have we learned today about strategy?