One, Concept:
The policy pattern defines a series of algorithms, encapsulates each algorithm, and allows them to replace each other, allowing the algorithm to vary independently from the customer using it.
Second, use the scene
1. The multiple ways of dealing with the same type of problem are only when the specific behavior is different;
2. The need to safely encapsulate a variety of operations of the same type;
3. There are multiple subclasses of the same abstract class, and you need to use If-else or switch-case to select a specific subclass.
Three, class diagram
Environment (context) Role: holds a reference to a strategy.
Abstract policy (Strategy) role: This is an abstract role, usually implemented by an interface or abstract class. This role gives the interfaces required for all the specific policy classes.
Specific policy (concretestrategy) Role: Wraps the associated algorithm or behavior.
Four, code example
Protocol Cardinterface {var money:float{get set} var discountshopping:float{get} var Discountfood:float{get} var discountrecreation:float{get} func Shopping (standardcost:float), Bool func food (Standardcost:floa T), Bool func Recreation (standardcost:float), Bool}class basecard:cardinterface {var money:float va R discountshopping:float var discountfood:float var discountrecreation:float init (money:float, dShopping: Float, Dfood:float, drecreation:float) {Self.money = Money discountshopping = dshopping discountf Ood = Dfood discountrecreation = drecreation} func shopping (standardcost:float), Bool {if Money >= StandardCost * discountshopping {money-= StandardCost * discountshopping print ("succes S:price (\ (standardcost)), Cost (\ (StandardCost * discountshopping)) in Fact,left (\ (Money)), type shopping ") re Turn true} Print ("Lack of Balance") return false} Func food (standardcost:float), Bool {if money = StandardCost * Discountfood {money-= StandardCost * Discountfood print ("Success:price (\ (Standa Rdcost)), Cost (\ (StandardCost * discountfood)) in Fact,left (\ (Money)), type food ") return true} Print ("Lack of Balance") return false} Func recreation (standardcost:float), Bool {if Mone Y >= StandardCost * discountrecreation {money-= StandardCost * discountrecreation print ("succes S:price (\ (standardcost)), Cost (\ (StandardCost * discountrecreation)) in Fact,left (\ (Money)), type Recreation ") return true} print ("Lack of Balance") return false}}class Nomalcard:basecard {init (money: Float) {super.init (Money:money, dshopping:0.88, dfood:0.9, drecreation:0.8)}}class Vipcard:basecard { Init (money:float) { Super.init (Money:money, dshopping:0.8, dfood:0.8, drecreation:0.7)}}class supervipcard:basecard {init (mo Ney:float) {super.init (Money:money, dshopping:0.7, dfood:0.75, drecreation:0.5)}}
Enum Cardtype:string {case nomal case VIP case supervip}class Customer { var card:cardinterface? var cardtype:cardtype Init (ctype:cardtype) { Cardtype = CType addcard () } fileprivate func Addcard () { switch cardtype {case . Nomal: card = Nomalcard (money:100) case . VIP: card = Vipcard (money:100) case . SUPERVIP: card = Supervipcard (money:100) default:break } } }
Class Viewcontroller:uiviewcontroller { override func Viewdidload () { super.viewdidload () let xiaoming = Customer (cType:. SUPERVIP) var rel = Xiaoming.card?. Recreation (standardcost:88) print (rel. false) rel = Xiaoming.card?. Recreation (standardcost:100) print (rel. false) rel = Xiaoming.card?. Recreation (standardcost:100) print (rel.? false) }}
Design mode-(17) Policy mode (Swift version)