Introduction to Strategy, strategy
I. Introduction to Strategy
The rule mode is the action mode.
Behavior mode:Defines the responsibilities of each object and the communication mode between objects. It standardizes the call and data transmission methods between objects.
The rule mode is suitable for situations where algorithms change frequently.
The algorithm changes will not affect the customers who use the algorithm. The algorithm can be independent of the customers who use the algorithm.
Ii. Simple Example
Ordinary customers, members, and VIP members purchase different prices.
Abstract class
Package strategy;/** abstract class for commodity price calculation */public abstract class Account {abstract public double getPrice (int amount, double d );}
Normal
Package strategy;/*** commodity price calculation for common customers */public class CommonAccount extends Account {@ Override public double getPrice (int amount, double price) {return amount * price ;}}
Member
Package strategy;/*** calculate the commodity price of ordinary members. */public class InsiderAccount extends Account {@ Override public double getPrice (int amount, double price) {return amount * price * 9/10 ;}}
VIP
Package strategy;/*** calculate the commodity price of VIP members */public class VipAccount extends Account {@ Override public double getPrice (int amount, double price) {return amount * price * 8/10 ;}}
Context
Package strategy;/*** this class is used to maintain reference of the Policy class */public class Context {// reference private Account account Account; // initialize public Context (account Account account) {this. account = account;} // call the calculation method public double doAccount (int amount, double d) {return account. getPrice (amount, d );}}
Test class
Package strategy;/*** this policy mode Test class */public class Test {public static void main (String [] args) {// declare the product String name = "DVD "; int amount = 2; double price = 50; double sum = 0; // declare the object // normal customer Context context = new Context (new CommonAccount (); sum = context. doAccount (amount, price); System. out. println ("ordinary Customer: no discount, purchased product name:" + name + "Quantity:" + amount + "unit price:" + price + "Payable amount: "+ sum); // normal member context = new Context (new InsiderAccount (); sum = context. doAccount (amount, price); System. out. println ("ordinary member: Off, purchased product name:" + name + "Quantity:" + amount + "unit price:" + price + "Payable amount: "+ sum); // VIP context = new Context (new VipAccount (); sum = context. doAccount (amount, price );
System. out. println ("VIP Customer: off, purchased product name:" + name + "Quantity:" + amount + "unit price:" + price + "Payable amount: "+ sum );}}
Result:
Ordinary customers: no discount, purchased product name: Number of DVDs: 2 unit price: 50.0 amount payable: 100.0 ordinary members: discount, purchased product name: Number of DVDs: 2 unit price: 50.0 payable amount: 90.0VIP customers: 50.0 off, purchased product name: Number of DVDs: 2 unit price: 80.0 payable amount: