Java design pattern: Strategy (policy pattern) and policy pattern (generalized strategy pattern)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.