Docsity

Strategy Pattern – Lecture Slides | CMSC 433, Study notes of Programming Languages

Toggle sidebar
bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Strategy Pattern
Apr. 19, 2007
2
What is it?
• The Strategy pattern is much like the State pattern in outline, but a little differe nt in
intent.
• The Strategy pattern consists of a number of related algorithms encapsulated in a
driver class called the Context.
• Your client program can select one of these differing algorithms or in some cases the
Context might select the best one for you.
• The intent, like the State pattern, is to switch easily between algorithms without any
monolith ic conditional s tatements.
• The difference between State and Strategy is that the user generally chooses which
of several strategies to apply and that only one strategy at a time is likely to be
instantiated and active within the Context class.
• By contrast, as we have seen, it is likely that all of the different States will be active
at once and switching may occur frequently between them.
• In addition, Strategy encapsulates several algorithms that do more or less the same
thing, while State encapsulates related classes that each do something somewhat
different.
• Finally, the concept of transition between different state s is completely missing in
the Strategy pattern.
3
Use it when…
• A program requires a particular service or function and which has
several ways of carrying out that function is a candidate for the
Strategy pattern.
• Programs choose between these algorithms based on computational
efficiency or user choice.
• There can be any number of strategies and more can be added and any
of them can be changed at any time.
• The idea behind Strategy is to encapsulate the various strategies in a
single module and provide a simple interface to allow choice between
these strategies.
– Each of them should have the same programming interface, although they
need not all be members of the same class hierarchy.
– However, they DO have to implement the same programming interface.
4
Use it when…
• There are a number of cases in programs where we’d like
to do the same thing in several different ways.
– Save files in different formats .
– Compress files using different algorithms
– Capture video data using different compression schemes
– Use different line-breaking strategies to display text data.
– Plot the same data in different formats: line graph, bar chart or pie
chart.
• In each case we could imagine the client program telling a
driver module (Context) which of these strategies to use
and then asking it to carry out the operation.
pf3
pf4
pf5

Partial preview of the text

Download Strategy Pattern – Lecture Slides | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Strategy Pattern Apr. 19, 2007

What is it?

intent.The Strategy pattern is much like the State pattern in outline, but a little different in

driver class called the Context.The Strategy pattern consists of a number of related algorithms encapsulated in a

Context might select the best one for you.Your client program can select one of these differing algorithms or in some cases the

monolithic conditional statements.The intent, like the State pattern, is to switch easily between algorithms without any

instantiated and active within the Context class.of several strategies to apply and that only one strategy at a time is likely to beThe difference between State and Strategy is that the user generally chooses which

at once and switching may occur frequently between them.By contrast, as we have seen, it is likely that all of the different States will be active

different.thing, while State encapsulates related classes that each do something somewhatIn addition, Strategy encapsulates several algorithms that do more or less the same

the Strategy pattern.Finally, the concept of transition between different states is completely missing in

3

Use it when…

Strategy pattern.several ways of carrying out that function is a candidate for theA program requires a particular service or function and which has

efficiency or user choice.Programs choose between these algorithms based on computational

of them can be changed at any time.There can be any number of strategies and more can be added and any

these strategies.single module and provide a simple interface to allow choice betweenThe idea behind Strategy is to encapsulate the various strategies in a

need not all be members of the same class hierarchy.Each of them should have the same programming interface, although they

However, they DO have to implement the same programming interface.

Use it when…

to do the same thing in several different ways.There are a number of cases in programs where we’d like

Save files in different formats.

Compress files using different algorithms

Capture video data using different compression schemes

Use different line-breaking strategies to display text data.

chart.Plot the same data in different formats: line graph, bar chart or pie

and then asking it to carry out the operation.driver module (Context) which of these strategies to useIn each case we could imagine the client program telling a

5

For Example

graph or a bar chart.present data as a linegraphing program that canConsider a simplified

classes from it.and derive the two plottingabstract PlotStrategy classWe’ll start with an

PlotStrategy Code

PlotStrategy class is derived from JFrame.Because each plot will appear in its own frame, our base

7

Notes…

must implement a method calledThe important part is that all of the derived classes

plot

with two

float arrays as arguments.

is appropriate.Each of these classes can do any kind of plot that

The Context

which strategy is to be called.The Context class is the traffic cop that decides

strategy or another.to do is to set a variable to refer to one concretethe client program, and all that the Context needsThe decision is usually based on a request from

13

Line Graph Button Class

specific for that display panel.Set up the window size for plotting and call a plot method

15

Notes

the plot command event.lines on the screen in direct response todon’t actually write a routine that drawsSince Java GUI is event-driven, you

Instead you provide a panel whose

paint

event is called. event carries out the plotting when that

The

repaint()

method invocation ensures that

it will be called right away.

the actual line and bar plots.JPanel and derive two classes from it forWe create a PlotPanel class based on

PlotPanel

code for scaling the data to the window.The base PlotPanel class contains the common

17

PlotPanel (2)

PlotPanel (3)

19

The Derived Classes

paint The two derived classes simply implement the

method for the two kinds of graphs.

Notes…

windows.program to exit if we close one of these subsidiaryJxFrame class, because we don’t want the entireNote that we don’t derive it from our special

hides the window if it is closed.Instead, we add a WindowAdapter class that just