Strategy Mode
Definition:
Runtime Selection Algorithm
On the surface, the state mode and the Strategy Mode are very similar, but since they are divided into two different modes, there are some differences. In a word, they are summarized as follows:
The policy is used to process algorithm mode changes, while the State is used to process state changes.
Specific can refer to: http://blog.csdn.net/lwzcjd/archive/2009/05/19/4201828.aspx
The following is an example of strategy:
Package com. Factory. commonality; <br/>/** <br/> * Strategy Mode Application. <Br/> */<br/> interface Strategy {<br/> void algorithminterface (); <br/>}< br/> class concretestrategya implements Strategy {<br/> Public void algorithminterface () {<br/> system. out. println ("called concretestrategya. algorithminterface () "); <br/>}< br/> class concretestrategyb implements Strategy {<br/> Public void algorithminterface () {<br/> system. out. println ("called concretestrategyb. algorithminterface () "); <br/>}< br/> class concretestrategyc implements Strategy {<br/> Public void algorithminterface () {<br/> system. out. println ("called concretestrategyc. algorithminterface () "); <br/>}< br/> class context {<br/> strategy Strategy; <br/> public context (strategy Strategy) {<br/> This. strategy = strategy; <br/>}< br/> Public void doalgorithm () {<br/> strategy. algorithminterface (); <br/>}< br/> public class strategypatterntest {<br/> Public static void main (string [] ARGs) {<br/> context C1 = new context (New concretestrategya (); <br/> c1.doalgorithm (); <br/> context C2 = new context (New concretestrategyb (); <br/> c2.doalgorithm (); <br/> context C3 = new context (New concretestrategyc ()); <br/> c3.doalgorithm (); <br/>}< br/>}
Running result:
Called concretestrategya. algorithminterface ()
Called concretestrategyb. algorithminterface ()
Called concretestrategyc. algorithminterface ()
Policy Mode
Although gof says that the policy mode is only an alias of The Strategy Mode, however, their example of the Strategy pattern implies that the strategy object uses only one method-that is, assuming that you have split the algorithm that may change into a separate code.
Other people use the policy mode to indicate that an object has multiple methods. For different classes, these methods may change independently of each other. Compared with (Strategy Mode), the Policy mode has more flexibility than only one method.
For example, when a product needs to be shipped to different countries, shipping policies related to the product can be used to describe shipping problems. These problems may include transportation methods, calculation of postage or freight, customer requirements and costs, and special handling costs. All these things may change independently, and it is important that you may need the above information in different locations during transportation.
Generally, it is advantageous to distinguish the Strategy Mode from the policy mode. The strategy mode is used to process a method (change), while the policy mode is used to process multiple methods.