Design Mode-policy Mode

Source: Internet
Author: User

Design Mode-policy Mode
Scenario settings

Design a calculator with the following options: +,-, *, And ,-,*,/.

The main idea of the Policy mode is to encapsulate all the available algorithms and pass them in and call them through a unified container. For example:
I have an interface for computing. I insert a calculator into it, and there are calculators, reducers, and so on in the calculator. These are policies. I wrap the policies and put them into the calculator for calling, insert the calculator interface.
Therefore, the code based on this mode should be as follows:
Computing interface:

interface Operation{    public int calculate(int a, int b);}

Policy implementationStrategyThe interface keeps all policies of the same type and has a unified call method:

class AddStrategy implements Strategy{    @Override    public int operate(int a, int b) {        return a+b;    }}class MinusStrategy implements Strategy{    @Override    public int operate(int a, int b) {        return a-b;    }}

Calculator should be implementedOperationInterface, used to executecalculate:

class Calculator implements Operation{    private Strategy strategy = null;    public void setStrategy(String tag){        if(tag.equals("+")){            strategy = new AddStrategy();        }        if(tag.equals("-")){            strategy = new MinusStrategy();        }    }    @Override    public int calculate(int a, int b) {        return strategy.operate(a,b);    }}

Finally, the following code calls the calculator:

 public static void main(String[] args){        Calculator calculator = new Calculator();        calculator.setStrategy("+");        calculator.calculate(1,2);    }

Rule mode is used to encapsulate algorithms. In practice, it can be used to encapsulate almost any type of rules. As long as you hear that different business rules need to be applied at different times during the analysis process, you can consider the possibility of using the Policy mode to handle such changes.
In the Basic Policy mode, the responsibility for selecting the specific implementation is borne by the client object and transmitted toCalculateObject.
However, for policy addition and modification, you still need to modify the Calculate class. The so-called any change requires cost.

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.