What?
Define a family of algorithms, encapsulate each one, and
Make them interchangeable. Strategy lets the algorithm
Vary independently from the clients it. --gof
Define a series of algorithms, encapsulate each one, and make each package interchangeable with each other. The strategy model replaces this series of algorithmic algorithms in a way that is independent of the client using these algorithms. Free to replace each algorithm without being bound by the client.
Official Website Example:
Public class taskcontroller { Public void Process() {//This code was an emulation of a //Processing Task Controller // . . . //figure out which countryCalctax MyTax; mytax= Gettaxrulesforcountry (); SalesOrder myso=NewSalesOrder (); Myso.process (MyTax); }PrivateCalctaxGettaxrulesforcountry() {//In real life, get the tax rules based on //Country you is in. the //Logic here or we have it in a //configuration file //Here, just return a ustax so this //would compile. return NewUstax (); }} Public class SalesOrder { Public void Process(Calctax taxtouse) {LongItemnumber=0;DoublePrice=0;//Given the tax object to use // . . . //Calculate tax DoubleTax = Taxtouse.taxamount (Itemnumber,price); }} Public Abstract class calctax { Abstract Public Double TaxAmount(LongItemsold,DoublePrice);} Public class cantax extends calctax { Public Double TaxAmount(LongItemsold,DoublePrice) {//In real life, figure out tax according to //The rules in Canada and return it //Here, return 0 so this would compile return 0.0; }} Public class ustax extends calctax { Public Double TaxAmount(LongItemsold,DoublePrice) {//In real life, figure out tax according to //The rules in the US and return it //Here, return 0 so this would compile return 0.0; }}
The Strategy Pattern:key Features (Key Features)
Intent (Purpose):
Allows you to use different business logic or algorithms depending on the circumstances (context).
Allows different business rules or algorithms depending
Upon the context in which they occur.
problem (Application scenario):
How to choose an appropriate, matching algorithm depends on the execution of the client's request or logic. If your selection algorithm is unique, you do not need strategy mode.
When analyzing to choose different business scenarios according to different situations, consider the following strategy!
The selection of an algorithm, needs to be applied depends upon
The client making the request or the data being acted upon. If You
Simply has a rule in place the does not the change, and you does not need a
Strategy pattern.
solution (Specific program):
The selection of the algorithm is separated from the implementation of the algorithm. Allows the selection of the algorithm according to the context.
Separates the selection of algorithm from the implementation of the
Algorithm. Allows for the selection to be made based upon context.
-
participants and collaborators:
strategy policy clarifies how different algorithms are used.
1. Each specific stragety class implements each specific algorithm (or business logic).
2. Contextual context selects a specific algorithm based on a reference to a strategy type ( Stragety). That is, the selection of the algorithm is the interaction result of the context and the stragety type.
3.Context forwards the request from Clinet to stragety.
The strategy specifies how the different algorithms is used.
? The concretestrategies implement these different algorithms.
? The Context uses the specific concretestrategy with a reference of
type strategy. The strategy and Context interact to implement the
chosen algorithm (sometimes the strategy must query the Context).
? The Context forwards requests from their Client to the strategy.
consequences (effect):
1. Strategy pattern defines a series of algorithms (Business logic)
2. Remove the switch, if Else statement.
3. All algorithms are called in the same way (because of polymorphism). The interaction of the algorithm implementation class and context contexts may require the context to invoke additional context.getstate ().
4. Each stragety implementation is a separate class that simplifies testing.
? The strategy pattern defines a family of algorithms.
? Switches and/or conditionals can be eliminated.
? You must invoke all algorithms in the same (they must all has the same interface). The interaction between the concretestrategies and the Context may require the addition of getstate type methods to the Co ntext.
Implementation
let the class that uses the algorithm (Context) contain an abstract class (strategy), in which the abstract class
There is an abstract method that specifies how the algorithm is called. Each derived class implements the algorithm as needed.
Note: In prototype Stragety mode, the responsibility for selecting the specific implementation class is assumed by the client object and transferred to the context object in the strategy mode.
The class that is uses the algorithm (the Context) contain an
Abstract class (the Stragegy) that have an abstract method pecifying
How to call the algorithm. Each derived class implements the algorithm
As needed. Note:this method wouldn ' t is abstract if you wanted to
has some default behavior. Note:in the prototypical strategy
pattern, the responsibility for selecting the particular
Implementation to use are done by the Client object and are given to the
Context of the strategy pattern.
- Coupling of strategy with context
The strategy pattern requires that the algorithms that are encapsulated must be outside the class (Context) in which they are used. That is to say that it either passes the information strategy needs to it (strategy), or it is obtained in other ways.
Disadvantages:
Have to create multiple additional classes. However, if you do not consider an extension algorithm, you can include all the algorithms in an inner class in the abstract class **strategy.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Design Pattern explained Reading notes four--strategy