



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1
Strategy Pattern Apr. 19, 2007
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
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.
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
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 class is derived from JFrame.Because each plot will appear in its own frame, our base
7
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
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
specific for that display panel.Set up the window size for plotting and call a plot method
15
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
code for scaling the data to the window.The base PlotPanel class contains the common
17
19
paint The two derived classes simply implement the
method for the two kinds of graphs.
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