Java design mode policy (strategy) mode

Source: Internet
Author: User

Strategy mode in the actual use of a lot of, first look at a scene: a market personnel received Tan Re after the bidding strategy, insurance policy is very complex, but can simply do the following classification:

New Customer Small Volume quote
New Customer High Volume quote
Low-volume quotation for old customers
High-volume quotation for old customers

The specific choice of which bidding strategy, which needs to be determined according to the actual situation, this time using the strategy model can solve the problem. In this question, what happens if we do not adopt a strategy model? It is natural to think of using if judgment, or switch...case ... To solve, according to different users to determine the different quotations. A structure similar to the following:

public  double  getprice  (String type,  Double  price) {if  (type.equals ( "New customer small Lot"     ) {//processing quotation logic  return  price; } else  if  (Type.equals (  "New Customer high Volume" ))    {//processing quotation logic  return  price; } //... }  

or use switch....case ...., but obviously there is a disadvantage, this type is very much, and each type has its own algorithm, if the algorithm is more complex, the entire condition of the control code will become very long, difficult to maintain. In order to solve this problem, we can use the policy mode.
Strategy mode: That corresponds to solve a problem of an algorithm family, allowing the user from the algorithm family candidate an algorithm to solve a problem, but also convenient to replace the algorithm or add a new algorithm, and by the client decide which algorithm to call, can be as follows:

First there is a policy interface, and then different four different strategies to implement the method in this interface, here is the method of quoting, and then define a context class, to manipulate specific specific policies, the client only needs to deal with this context, that is, as long as the policy to the context class, you can execute the corresponding policy , if you need to modify or add later, only need to modify the specific policy implementation class or add a new implementation class, easy to maintain. Let's look at the implementation of the program below.

//策略接口publicinterface Strategy {    publicdoublegetPrice(double standardPrice);}

Four different policy implementation classes:

//New Customer small batch Public  class newcustomerfewstrategy implements strategy {    @Override     Public Double GetPrice(DoubleStandardprice) {System.out.println ("New Customer small batch, no discount");returnStandardprice; }}//New customers in large quantities Public  class newcustomermanystrategy implements strategy {    @Override     Public Double GetPrice(DoubleStandardprice) {System.out.println ("New customers in large quantities, hit 90 percent");returnStandardprice *0.9; }}//Old customers small batch Public  class oldcustomerfewstrategy implements strategy {    @Override     Public Double GetPrice(DoubleStandardprice) {System.out.println ("Old customer small batch, dozen 80 percent");returnStandardprice *0.8; }}//Old customers in large quantities Public  class oldcustomermanystrategy implements strategy {    @Override     Public Double GetPrice(DoubleStandardprice) {System.out.println ("Old customers in large quantities, hit 80 percent");returnStandardprice *0.7; }}

Then there is the context class:

/** * @Description responsible for interacting with specific strategies, specific algorithms and direct client separation. * @author Ni SHENGWU * */ Public  class Context {    PrivateStrategy strategy;//through the construction method to inject the specific strategy, if using spring, you can directly use @resource to inject the     Public Context(Strategy strategy) {Super(); This. strategy = strategy; } Public void GetPrice(DoubleStandardprice) {System.out.println ("Quote for:"+ Strategy.getprice (standardprice)); }}

Test it:

publicclass Client {    publicstaticvoidmain(String[] args) {        new OldCustomerManyStrategy();        new//通过构造方法注入策略        ctx.getPrice(1000);    }}

The output is:

Old customers in large quantities, hit 80 percent
Offer: 700.0

In practice, the policy pattern is also useful in spring, with annotations @resource using that pattern, which is injected with which.
  

-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15

Java design mode policy (strategy) mode

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.