Analysis of Android source code design patterns and practices (7)
Chapter 7 Policy Model
Generally, if there are multiple solutions to a problem, the simplest solution is to use the if-else or switch-case method to select different solutions based on different scenarios, however, the coupling is too high, the code is bloated, and it is difficult to maintain. In this case, you can use the policy mode to solve the problem.
1. Definition
Rule patterns define a series of algorithms, encapsulate each algorithm, and enable them to replace each other. The rule mode allows algorithms to change independently of customers who use it.
2. Use Cases
1. Multiple handling methods for the same type of problems are provided only when specific actions are different.
2. When multiple operations of the same type need to be securely encapsulated.
3. When the same abstract class has multiple subclasses, you must use if-else or switch-case to select the specific subclass.
3. Simple implementation
Demand: Calculate the book price. There is no discount for junior members, for intermediate members, and for senior members. In general, if-else should determine the level of membership and calculate the corresponding discount. The following uses the policy mode for implementation.
Abstract discount
Public interface MemberStrategy {/*** calculate the book price * @ param booksPrice original price * @ return calculate the discounted price */public double calcPrice (double booksPrice );}
Discount for junior members
Public class PrimaryMemberStrategy implements MemberStrategy {/*** junior member Discount */@ Override public double calcPrice (double booksPrice) {System. out. println ("No discounts for junior members"); return booksPrice ;}}
Intermediate member discount
Public class IntermediateMemberStrategy implements MemberStrategy {/*** intermediate member Discount */@ Override public double calcPrice (double booksPrice) {System. out. println ("10% discount for intermediate members"); return booksPrice * 0.9 ;}}
Discount for senior members
Public class AdvancedMemberStrategy implements MemberStrategy {/*** */@ Override public double calcPrice (double booksPrice) {System. out. println ("20% discount for senior members"); return booksPrice * 0.8 ;}}
Price
Public class Price {// hold a specific policy object private MemberStrategy strategy;/*** constructor, input a specific policy object * @ param strategy specific policy object */public Price (MemberStrategy strategy) {this. strategy = strategy;}/*** calculate the book price * @ param booksPrice original price * @ return calculate the discounted price */public double quote (double booksPrice) {return this. strategy. calcPrice (booksPrice );}}
Client
Public class Client {public static void main (String [] args) {// select and create the policy object MemberStrategy strategy1 = new AdvancedMemberStrategy (); // create the environment Price price = new Price (strategy1); // calculate the price double quote = Price. quote (1, 300); System. out. println ("the final price of the book is:" + quote );}}
Result
The final price for a discount of 20% for senior members is: 240.0
4. Implementation of the Policy mode in the Android Source Code 1. TimeInterpolator)
LinearInterpolator, AccelerateInterpolator, CycleInterpolator, and other implementation Interpolator. Get the current time percentage through getInterpolator (float input) to calculate the animation attribute value.
5. Summary
The rule mode is mainly used to separate algorithms and has different implementation policies under the same Behavior Abstraction. This mode demonstrates the principle of opening/closing, that is, defining abstraction and injecting different implementations to achieve good scalability.
Advantages:
1. Clear structure, simple and intuitive to use.
2. Relatively low coupling, convenient extension.
3. Operation encapsulation is more thorough and data is more secure.
Disadvantages:
1. As the policy increases, the number of child classes also increases.