18
Stéphane Ducasse 1 Stéphane Ducasse [email protected] http://stephane.ducasse.fr ee.fr/ Strategy

Stoop 436-strategy

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Stoop 436-strategy

Stéphane Ducasse 1

Stéphane [email protected]://stephane.ducasse.free.fr/

Strategy

Page 2: Stoop 436-strategy

S.Ducasse 2

Strategy

Define a family of algorithms, encapsulate each in a separate class and define each class with the same interface so that they can be interchangeable.

Also Know as Policy

Page 3: Stoop 436-strategy

S.Ducasse 3

Strategy Intent

Define a family of algorithms, encapsulate each in a separate class and define each class with the same interface so that they can be interchangeable.

Page 4: Stoop 436-strategy

S.Ducasse 4

Motivation

Many algorithms exist for breaking a stream into lines. Hardwiring them into the classes that requires them has the following problems:

Clients get more complexDifferent algorithms can be used at different timesDifficult to add new algorithms at run-time

Page 5: Stoop 436-strategy

S.Ducasse 5

Code Smells

Composition>>repair formatting == #Simple ifTrue: [ self formatWihtSimpleAlgo] ifFalse: [ formatting == #Tex ifTrue: [self formatWithTex]

....]

Page 6: Stoop 436-strategy

S.Ducasse 6

Alternative

Composition>>repair | selector | selector := (‘formatWith, formatting) asSymbol. self perform: selector

Still your class gets complex...

Page 7: Stoop 436-strategy

S.Ducasse 7

Inheritance?

May not be the solution since:- you have to create objects of the right class- it is difficult to change the policy at run-time- you can get an explosion of classes bloated with the use of a functionality and the functionalities.- no clear identification of responsibility

Page 8: Stoop 436-strategy

S.Ducasse 8

Strategy Solution

Page 9: Stoop 436-strategy

S.Ducasse 9

When

Many related classes differ only in their behaviorYou have variants of an algorithm (space/time)An algorithm uses data that the clients does not have to know

Page 10: Stoop 436-strategy

S.Ducasse 10

Structure

Composition>>repair formatter format: self

Page 11: Stoop 436-strategy

S.Ducasse 11

Participants

Strategy (Compositor)declares an interface common to all concrete strategies

Concrete Strategiesimplement algorithm

Contextconfigure with concrete strategymaintains a reference to the concrete strategymay define an interface to let the strategy access data

Page 12: Stoop 436-strategy

S.Ducasse 12

Collaborations (i)

Strategy and Context interact to implement the chosen algorithm.

A context may pass all data required by the algorithm to the strategy when the algorithm is called

GraphVisualizer>>graphIt .... grapher plot: data using: graphPane pen

Grapher>>plot: data using: aPen

Page 13: Stoop 436-strategy

S.Ducasse 13

Context passes itself as argument

Also know as self-delegation...

GraphVisualizer>>graphIt grapher plotFor: self

BartChartGrapher>>plotFor: aGraphVisualizer |data| data := aGraphVisualizer data ....

Page 14: Stoop 436-strategy

S.Ducasse 14

BackPointer

Grapher class>>for: aGraphVisualizer ^ self new graphVisualizer: aGraphVisualizer

BartChartGrapher>>plot ... graphVisualizer data.. graphVisualizer pen

Grapher (Strategy) points directly to GraphVisualizer (Context), so sharing strategy between different context may be difficult, if sharing is needed then use self-delegation

Page 15: Stoop 436-strategy

S.Ducasse 15

Collaboration (ii)

“A context forwards requests from its clients to its strategy. Clients usually create and pass a ConcreteStrategy object to the context; thereafter, clients interact with the context exclusively. “ GOF

Not sure that the client has to choose...

Page 16: Stoop 436-strategy

S.Ducasse 16

Consequences

Define a family of pluggable algorithmsEliminates conditional statementsClients can choose between several implementationsClients must be aware of the different strategiesIncrease the number of objectsCommunication overhead between client and strategiesWeaken encapsulation of the client

Page 17: Stoop 436-strategy

S.Ducasse 17

Domain-Specific Objects as Strategies

Strategies do not have to be limited to one single algorithmThey may represent domain specific knowledge

MortgageFixedRateMortgageOneYear...

Page 18: Stoop 436-strategy

S.Ducasse 18

Known Uses

ImageRenderer in VW: “a technique to render an image using a limited palette”ImageRendererNearestPaintOrderedDitherErrorDiffusion

View-Controllera view instance uses a controller object to handle and respond to user input via mouse or keyboard. Controllers can be changed at run-time