Strategy Mode of design pattern

Source: Internet
Author: User

Intent: Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. The policy pattern allows the algorithm to change independently of the customers who use it.

1. Need to use the algorithm provided by Concretestrategy (specific policy role);

2. Maintain an instance of strategy (abstract policy Class) internally;

3. Responsible for the dynamic setting of the runtime strategy specific implementation algorithm;

4. Responsible for the interaction and data transfer with strategy.

Main Solution: the use of if...else is complicated and difficult to maintain in the case of many similar algorithms.

when to use: a system has many classes, and it is only their direct behavior that distinguishes them.

How to solve: encapsulate these algorithms into a single class and replace them arbitrarily.

Key code: implement the same interface.

Application Example: 1, Zhuge Liang's ace, every one of the Jin sac is a strategy. 2, Travel way, choose to ride a bike, ride a car, every way of travel is a strategy.

Advantages: 1, the algorithm can be switched freely. 2, avoid the use of multiple conditions to judge. 3, good Extensibility.

Cons: 1, each strategy will produce a new class, the policy class will increase. 2. All policy classes need to be exposed externally. Choosing which class requires the client to create the object increases the coupling.

usage Scenario: 1, if there are many classes in a system, the difference between them is only their behavior, then the use of policy mode can dynamically let an object in many behaviors to choose a behavior. 2, a system needs to dynamically select one of several algorithms. 3, if an object has a lot of behavior, if not the appropriate pattern, these behaviors have to use multiple conditional selection statements to achieve.

Note: If a system has more than four policies, you need to consider using mixed mode to solve the problem of policy class bloat.

1  Public Interface pricestrategy {2     double pricestrategyinterface (double price ); 3 }
 1  public  class  Supervipstrategt implements   Pricestrategy {  2   @Override  3  public  double  Pricestrategyinterface (double   price) { 4  ; 5   6  7 } 
 1  public  class  Goldvipstrategt implements   Pricestrategy {  2  3   @Override  4  public  double  pricestrategyinterface (double  " Span style= "color: #000000;"  > price) { 5  return  Price*0.9; 6   7  }
 1  public  class  Slivervipstrategy implements   Pricestrategy {  2  3   @Override  4  public  double  pricestrategyinterface (double  " Span style= "color: #000000;" > price) { 5  return  Price*0.95
   
    ; 
    6  
     7  }
   
 Public class Price {    private  pricestrategy pricestrategy;      Public Price   (Pricestrategy pricestrategy) {        this.pricestrategy=pricestrategy;        }     publicdouble  getvipprice (double price ) {        return  pricestrategy.pricestrategyinterface (price);}    }
 Public classTest { Public Static voidMain (string[] args) {supervipstrategt supervipstrategt=NewSupervipstrategt (); Goldvipstrategt Goldvipstrategt=NewGoldvipstrategt (); Slivervipstrategy Slivervipstrategy=NewSlivervipstrategy (); System.out.println ("Super member Price =" +NewPrice (SUPERVIPSTRATEGT). Getvipprice (100)); System.out.println ("Gold member Price =" +NewPrice (GOLDVIPSTRATEGT). Getvipprice (100)); System.out.println ("Silver member Price =" +NewPrice (Slivervipstrategy). Getvipprice (100)); }}

Super member Price =80.0
Gold member Price =90.0
Silver Member Price =95.0

Strategy Mode of design pattern

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.