Strategy ):It defines a series of algorithms, encapsulates each algorithm, and enables them to replace each other. The policy pattern prevents algorithm changes from affecting customers who use the algorithm. (Original article: The Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it .)
Application scenarios: 1. Multiple classes only differ in different performance behaviors. You can use the Strategy Mode to dynamically select the behavior to be executed during running.
2. Different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future.
3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.
For a small example of the policy model, we all know that there are often various discounts in supermarkets or online malls. If we write a toll computing system, how can we design this code? Because of the various situations and differences in discount activities, how can I write a code change that reduces the probability of a business trip when the discount activity is modified?
Think about using the following policy mode:
First, let's take a look at the discounted interface that can be released independently:
public interface IDiscount {double getPrice(double price, int quantity);}
The following are two discount methods for implementing interfaces: 1. Direct discount
public class Rebate implements IDiscount {private double discount;public void setDiscount(double discount){this.discount = discount;}public double getDiscount(){return discount;}@Overridepublic double getPrice(double price, int quantity) {// TODO Auto-generated method stubreturn price*quantity*discount;}}
II. A certain amount of cash return
public class CashReturn implements IDiscount {private int cashCondition;private int cashReturn;public void setCondition(int cashCondition,int cashReturn){this.cashReturn = cashReturn;this.cashCondition = cashCondition;}@Overridepublic double getPrice(double price, int quantity) {double orignal = price * quantity;int n = (int) (orignal / cashCondition);return orignal - cashReturn * n;}}
Cashier:
Public class cashregister {private idiscount; Public void setdiscount (idiscount) {This. idiscount = idiscount;} public void getaccountreceivable (double price, int quantity) {system. out. println ("receivables:" + idiscount. getprice (price, quantity ));}}
Test:
Public static void main (string [] ARGs) {// two discount Methods: idiscount rebate = new rebate (); idiscount cashreturn = new cashreturn (); cashregister = new cashregister (); // use discount (rebate ). setdiscount (0.8); cashregister. setdiscount (rebate); cashregister. getaccountreceivable (102.8, 3); // cash return (cashreturn ). setcondition (200,40); cashregister. setdiscount (cashreturn); cashregister. getaccountreceivable (102.8, 3 );}
Result:
Receivables: 246.72
Receivables: 268.4
Here, I think about how to separate the changeable part and use the delegate to decouple it. My entity completes the fixed function. Use the policy class to implement the functions to be adjusted.
Advantages:
- It provides an alternative to inheritance, and maintains the advantages of inheritance (code reuse) and is more flexible than inheritance (algorithms are independent and can be expanded at will ).
- Avoid using multiple conditional transfer statements in the program to make the system more flexible and easy to expand.
- Comply with most grasp principles and common design principles, high cohesion and low coupling.
Disadvantages: 1. Because each specific policy class generates a new class, the number of classes to be maintained by the system is increased.
Use in Android
Listview. setadapter (), where the adapter is generally a custom adapter that inherits from the baseadapter. This is a typical policy mode. When the listview item is displayed in different forms, in the getview method, different implementations are required.