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)