Design Pattern Explained Reading Notes 4 -- Strategy, patternexplained
What?
Define a family of algorithms, encapsulate each one, and
Make them interchangeable. Strategy lets the algorithm
Vary independently from the clients that use it. -- GOF
Define a series of algorithms, encapsulate each and make each encapsulation replaceable. The Strategy Mode replaces these algorithms in a client independent of these algorithms. Each algorithm is freely replaced, and is not bound by the client.
Official Website instance:
public class TaskController { public void process () { // this code is an emulation of a // processing task controller // . . . // figure out which country you are in CalcTax myTax; myTax= getTaxRulesForCountry(); SalesOrder mySO= new SalesOrder(); mySO.process(myTax); } private CalcTax getTaxRulesForCountry() { // In real life, get the tax rules based on // country you are in. You may have the // logic here or you may have it in a // configuration file // Here, just return a USTax so this // will compile. return new USTax(); }}public class SalesOrder { public void process (CalcTax taxToUse) { long itemNumber= 0; double price= 0; // given the tax object to use // . . . // calculate tax double tax = taxToUse.taxAmount(itemNumber,price); }}public abstract class CalcTax { abstract public double taxAmount(long itemSold,double price);}public class CanTax extends CalcTax { public double taxAmount(long itemSold,double price) { // in real life, figure out tax according to // the rules in Canada and return it // here, return 0 so this will compile return 0.0; }}public class USTax extends CalcTax { public double taxAmount(long itemSold,double price) { // in real life, figure out tax according to // the rules in the US and return it // here, return 0 so this will compile return 0.0; }}
The Strategy Pattern: Key Features (Key Features)
Intent (Purpose ):
You can use different business logic or algorithms based on different environments (context.
Allows you to use different business rules or algorithms depending
Upon the context in which they occur.
Problem (Application Scenario ):
How to select an appropriate and matching algorithm depends on the client's request or logic execution. If your selection algorithm is unique, you do not need the Strategy Mode.
When the analysis shows that you need to select different business solutions based on different situations, consider Strategy!
The selection of an algorithm that needs to be applied depends
The client making the request or the data being acted upon. If you
Simply have a rule in place that does not change, you do not need
Strategy pattern.
Solution (specific Solution ):
Separate algorithm selection from algorithm implementation. You can select an algorithm based on the context.
Separates the selection of algorithm from the implementation of
Algorithm. Allows for the selection to be made based upon context.
Participant and Collaborators:
The Strategy policy defines how different algorithms are used.
1. Each specific Stragety class implements each specific algorithm (or business logic ).
2. Context selects a specific algorithm based on a reference of the Strategy type ( Stragety). That is to say, the algorithm selection is the interaction result of the Context and Stragety types.
3. Context transfers the request from Clinet to Stragety.
The strategy specifies how the different algorithms are used.
• The concreteStrategies implement these different algorithms.
• The Context uses the specific ConcreteStrategy with a reference
Type Strategy. The strategy and Context interact to implement
Chosen algorithm (sometimes the strategy must query the Context ).
• The Context forwards requests from its Client to the Strategy.
Consequences (effect ):
1. The Strategy Mode defines a series of algorithms (business logic)
2. Remove switch, if else, and other statements.
3. Call all algorithms in the same way (because of polymorphism ). The interaction between the Algorithm Implementation class and Context may require Context to call additional Context. getState ().
4. Each Stragety implementation is an independent class, which simplifies the test.
• The Strategy pattern defines a family of algorithms.
• Switches and/or conditionals can be eliminated.
• You must invoke all algorithms in the same way (they must all have the same interface). The interaction between the ConcreteStrategies and the Context may require the addition of getstate type methods to the Context.
Implementation
Let the class (Context) using the algorithm contain an abstract class (Strategy), which contains
There is an abstract method that specifies how to call an algorithm. Each derived class implements algorithms as needed.
Note: In the prototype Stragety mode, the responsibility for selecting the specific implementation class is borne by the Client object and transferred to the Context object in the Strategy Mode.
Have the class that uses the algorithm (the Context) contain
Abstract class (the stragegy) that has an abstract method pecifying
How to call the algorithm. Each derived class implements the algorithm
As needed. Note: this method wouldn't be abstract if you wanted
Have some default behavior. Note: In the prototypical Strategy
Pattern, the responsibility for selecting the participates
Implementation to use is done by the Client object and is given to
Context of the Strategy pattern.
- Coupling between Strategy and Context
The Strategy mode requires that the encapsulated algorithms must be outside the Context where they are used. That is to say, this either transmits the information required by Strategy to it or gets it in other forms.
Disadvantages:
You have to create multiple additional classes. However, if you do not consider extension algorithms, you can use an internal class to include all algorithms in the abstract class ** Strategy.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.