Strategy Mode of design pattern

Source: Internet
Author: User

Policy Mode

policy mode : Refers to a specified object has a behavior, but in different scenarios, the behavior has a different implementation way. by implementing the different implementations independently, and allowing them to be replaced by each other , so that the different ways independent, for the user to choose.

Uml

The following is a simple arithmetic operation using the policy pattern:

 package   strategy;  /**    @author   WxH * @vertion 1.0  */ public  interface   Operator { int  Execute (int  num1,  num2); }
 package   strategy;  /**   * addition Operation *   @author   WxH * @vertion 1.0  */ public  class  Plus implements   operator{@Override  Span style= "color: #0000ff;" >public  int  Execute (int  Num1, Span style= "color: #0000ff;" >int   num2) { return  num1 + num2; }}
 Package strategy; /**  @author*/Publicclassimplements  Operator {    @Override      Public int Execute (intint  num2) {        return num1- num2;    }} 
 Packagestrategy;/*** Calculator *@authorWxH * @vertion 1.0*/ Public classCalculator {//Number 1    Private intNumber1; //Number 2    Private intnumber2; //operator    PrivateOperator Operator; //return operation result     Public intGetResult () {returnOperator.execute (Number1, number2); }     Public intGetNumber1 () {returnNumber1; }     Public voidSetNumber1 (intnumber1) {         This. Number1 =Number1; }     Public intGetNumber2 () {returnnumber2; }     Public voidSetNumber2 (intnumber2) {         This. number2 =number2; }     PublicOperator Getoperator () {returnoperator; }     Public voidsetoperator (Operator Operator) { This. operator =operator; }    }
 Package strategy;  Public class Test {        publicstaticvoid  main (string[] args) {                new Calculator ();         );          );         // set operator        New Plus ());        System.out.println (Cal.getresult ());}            }

Execution Result: 50

Policy mode + Template method used together
/*** Calculator *@authorWxH * @vertion 1.0*/ Public classCalculator {Private intNumber1; Private intnumber2;  Public intGetNumber1 () {returnNumber1; }     Public voidSetNumber1 (intnumber1) {         This. Number1 =Number1; }     Public intGetNumber2 () {returnnumber2; }     Public voidSetNumber2 (intnumber2) {         This. number2 =number2; }    }
/*** Template Method *@authorWxH * @vertion 1.0*/ Public Abstract classOperator {PrivateCalculator Cal;  PublicOperator (Calculator cal) { This. Cal =Cal; }        protected Abstract intExecuteintNUM1,intnum2);  Public intDoexecute () {return Execute (cal.getnumber1 (), Cal.getnumber2 ()); }    }
 /**   * addition Operation *   @author   WxH * @vertion 1.0  */ Span style= "color: #0000ff;" >public  class  Plus extends   operator{ public   Plus (Calculator cal) {     super   (CAL); } @Override  protected  int  Execute (int  num1, int   num2)    { return  num1 + num2; } }
 Public classTest01 { Public Static voidMain (string[] args) {Calculator cal=NewCalculator (); Cal.setnumber1 (30); Cal.setnumber2 (20); Plus P=NewPlus (CAL);//addition//minus m = new Minus (CAL);//SubtractionSystem.out.println (P.doexecute ()); }    }

Execution Result: 50

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.