Java design mode policy mode (strategy)

Source: Internet
Author: User

Objective:

Recently has been learning based on the Okhttp network request, the process of learning is remembered in the previous project there is a need for a different interface to adopt different encryption methods, such as to use RSA encryption before logging in, to use AES encryption after login, It was used to pass a parameter in the past to the parameters of different encryption algorithm, which makes the code difficult to maintain and difficult to expand, then how to solve it? Learn about strategy mode today strategy! Use it to solve the problem.

Policy mode:

the policy mode belongs to the behavior pattern of the object. It is intended for a set of algorithms that encapsulate each algorithm in a separate class with a common interface, so that they can be replaced with each other. The policy pattern allows the algorithm to change without affecting the client.

Examples of how to use the policy mode:

Use different encryption methods for different requests in the previous project as an example.

1.) Do not adopt the Strategy mode implementation method
    enumEncrypttype{aes,rsa};  Public voiddorequest (encrypttype type, String params) {Switch(type) { CaseAES://AES Encryption algorithm is hereParams= "AES Encrypted over data";  Break;  CaseRSA://RSA encryption algorithm is performed hereParams= "Encrypted data over RSA";  Break; }       //then call the network request to send the parameters past}

The above code can be seen that if you want to continue to add other methods of encryption algorithm, only by increasing the Encrypttype enumeration type, and then increase the type of judgment, not only destroys the network request structure, but also caused a high coupling is not conducive to expansion.

2.) Use Policy mode first step: declaring an encrypted interface
 Public Interface iencryptstrategy {    string encryptstr (string params);}
3.) Use Policy mode Step two: Add a different policy

RSA mode

 Public class Implements iencryptstrategy {    @Override    public  string encryptstr (string params) {          Return "Encrypted data over RSA";    }}

AES Mode

 Public class Implements iencryptstrategy {    @Override    public  string encryptstr (string params) {         return "data that has been encrypted by AES";    }}
3.) Use Policy mode step three: choose a different strategy

Using policies

        Iencryptstrategy rsastrategy=New  rsastrategy ();        DoRequest (Rsastrategy,"test");

Receive policy

     Public void dorequest (iencryptstrategy encryptstrategy, String params) {        params=Encryptstrategy.encryptstr ( params);        // then call the network request to send the parameters past    }

We summarize the advantages and disadvantages of the strategy model through the above example:

Advantages:

1, simplified unit testing, because each algorithm has its own class, can be tested by their own interface alone.
2, avoid the use of multiple conditional transfer statements in the program, make the system more flexible, and easy to expand.
3, abide by most grasp principles and common design principles, high cohesion, low coupling.

Disadvantages:
1, because each specific policy class will produce a new class, it will increase the number of classes that the system needs to maintain.

2, in the basic strategy mode, select the specific implementation of the responsibility by the client object, and transferred to the policy mode of the context object, there is no cancellation of the client needs to choose the pressure of judgment

Summarize:

The use of policy mode is a good solution to the request encryption requirements that were encountered in the previous project, and is easier to scale.

Java design mode policy mode (strategy)

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.