Description: This is "Dahua design pattern" a Book of Learning abstracts and online related information Digest, the original book code examples in C # write, the following Java rewrite.
1. Policy pattern: Defines a series of algorithms, encapsulates each algorithm, and makes them interchangeable. The policy pattern allows the algorithm to vary independently from the customer who uses it.
2. The strategy model consists of three roles:
(1) Abstract Policy role: policy class, usually implemented by an interface or abstract class.
(2) Specific policy role: packaging related algorithms and behavior.
(3) Environment role: holds a reference to a policy class, which is eventually invoked to the client.
3, the UML class diagram of the policy pattern
4, algorithm implementation
Package Demo3;
/** * Abstract algorithm's policy class, defines the common interface of all supported algorithms * */abstract class Strategy {//algorithm method public abstract void Algorithminterface ();}
Package Demo3; /** * Specific Algorithm A * */public class Concretestrategya extends strategy {//algorithm a implementation method @Override public void Algorithminterfac
E () {SYSTEM.OUT.PRINTLN ("Implementation of Algorithm A");
}} package Demo3; /** * Specific Algorithm b * */public class Concretestrategyb extends strategy {///algorithm B implementation method @Override public void Algorithminterfa
CE () {System.out.println ("Implementation of Algorithm B");
}} package Demo3; /** * Specific Algorithm c * */public class CONCRETESTRATEGYC extends strategy {@Override public void Algorithminterface () {Sys
TEM.OUT.PRINTLN ("Implementation of Algorithm C");
}} package Demo3;
/** * Context, maintaining a reference to the policy class object * * * */public class Context {strategy strategy;
Public context (strategy strategy) {this.strategy = strategy; public void Contextinterface () {strategy.
Algorithminterface ();
}} package Demo3; /** * Client code: Implementing different Policies * */public class Demo3 {public static void main (StRing[] args) {context context;
Context = new Context (new Concretestrategya ());
Context.contextinterface ();
Context = new Context (new Concretestrategyb ());
Context.contextinterface ();
Context = new Context (new CONCRETESTRATEGYC ());
Context.contextinterface (); }
}
5, an example: Shopping malls cash register software, according to the market activities can be discounted, full how much less, with the strategy of simple factory implementation.
Package Demo2;
/** * Cash charge abstract class/public abstract classes Cashsuper {//parameter is the original price, return to the current price public abstract double Accptcash (double);}
Package Demo2; /** * Normal charge Sub class */public class Cashnormal extends Cashsuper {@Override public double Accptcash (double) {Retu
RN Money;
}} package Demo2;
/** * Discount Charges sub-class * * public class Cashrebate extends Cashsuper {private double moneyrebate = 1d;
Public cashrebate (double moneyrebate) {this.moneyrebate = moneyrebate;
@Override public double Accptcash (double) {return money * moneyrebate;
}} package Demo2;
/** * Rebate Charge sub-class * * public class Cashreturn extends Cashsuper {private double moneycondition = 0.0d;
Private double moneyreturn = 0.0d;
Public Cashreturn (double moneycondition, double moenyreturn) {this.moneycondition = moneycondition;
This.moneyreturn = Moenyreturn;
@Override public double Accptcash (double) {double: = money; if (Money >= moneycondition) result = MOney-math.floor (money/moneycondition) * Moneyreturn;
return result;
}} package Demo2;
/** * Strategy is combined with simple factory: The process of instantiating specific policies is transferred from client to here Context class */public class Cashcontext {private Cashsuper cs;
Public Cashcontext (String type) {switch (type) {case "normal charge": cs = new Cashnormal ();
Break
Case "Full 300 return": cs = new Cashreturn (300, 100);
Break
Case "hit 80 percent": cs = new Cashrebate (0.8);
Break
Public double getreslt (double) {return Cs.accptcash (money);
}} package Demo2; /** * Client uses */public class Demo2 {public static void main (string[] args) {//normal charge Cashcontext cc = new Cashcontex
T ("normal charge");
Double totalprices = cc.getreslt (500);//Original Price System.out.println (totalprices);
Full 300 cc = new Cashcontext ("Full 300 to 100");
Totalprices = CC.GETRESLT (500);//Original Price System.out.println (totalprices); }
}