Chapter Two: Market promotion--strategy mode
The definition of the policy pattern: The strategy pattern is a method to define a series of algorithms, conceptually, all of these algorithms do the same work, the knowledge implementation is different, he can call all the algorithms in the same way, reducing the advantages of various types of algorithm classes and the use of the coupling strategy model between the algorithm class: 1. The strategy class hierarchy of the policy pattern defines the reusable algorithm or behavior of some columns for the context. Inheritance helps to extract the public functions in these algorithms 2. Simplifies unit testing because each algorithm has its own class, which can be tested by its own interface. When different behaviors are stacked in a class, it is difficult to avoid using conditional statements to choose the right behavior. By encapsulating these behaviors in separate strategy classes, you can eliminate conditional statement policy patterns in classes that use these behaviors to encapsulate algorithms, but in practice we find that they can be used to encapsulate almost any type of rule, As long as you hear the need to apply different business rules in different practices during the analysis process, you can consider the possibility of using a policy model to handle this change in the basic policy model, selecting the specific implementation responsibilities that the client object undertakes, and transferring the context object to the policy mode
Parent class Cashsuper
CashSuper.h
@interface cashsuper:nsobject-(double) Acceptcash: (double) money; @end
Cashsuper.m
@implementation Cashsuper
-(double) Acceptcash: (double) money{
return money;
}
@end
Normal consumption class Cashnormal
CashNormal.h
#import <Foundation/Foundation.h>#import"CashSuper.h"@ Interface cashnormal:cashsuper@end
cashnormal.m
#import " CashNormal.h " @implementation cashnormal-(double) Acceptcash: (double) money{ return Money;} @end
Discounted consumption category Cashrebate
CashRebate.h
#import <Foundation/Foundation.h>#import"CashSuper.h"@ Interface cashrebate:cashsuper@property (assign,nonatomic)double rebate; -(Instancetype) Initwithrebate: (double) rebate; @end
Cashrebate.m
#import " @implementation Span style= "color: #000000;" > Cashrebate -(Instancetype) Initwithrebate: (double ) rebate{self = [super Init]; if =rebate; return self;} -(double ) Acceptcash: (double ) money{ return money*_rebate;} @end
Rebate Consumption class Cashreturn
CashReturn.h
#import <Foundation/Foundation.h>#import"CashSuper.h"@ Interface cashreturn:cashsuper@property (assign,nonatomic)double moneycondition;@ Property (assign,nonatomic)double moneyreturn; -(Instancetype) Initwithmoneycondition: (double) moneycondition:(double) Moneyreturn; @end
Cashreturn.m
#import "CashReturn.h"@implementationCashreturn-(Instancetype) Initwithmoneycondition: (Double) Moneycondition:(Double) moneyreturn{ Self=[Super Init]; if(self) {_moneycondition=moneycondition; _moneyreturn=Moneyreturn; } returnSelf ;}-(Double) Acceptcash: (Double) money{Doubleresult=Money ; if(money>=_moneycondition) {Result=money-((int) (money/_moneycondition)) *_moneyreturn; } returnresult;}@end
Configuration classes, maintaining the use of Objects Cashcontext class
CashContext.h
#import <Foundation/Foundation.h>#import"CashSuper.h"@ Interface*cashsuper; -(Instancetype) Initwith: (Cashsuper *) csuper; -(double) GetResult: (double) money; @end
Cashcontext.m
#import " CashContext.h " @implementation cashcontext-(Instancetype) Initwith: (Cashsuper *) csuper{ = [Super init]; if (self) { _cashsuper=csuper; } return Self ;} -(double) GetResult: (double) money{ return [_ Cashsuper Acceptcash:money];} @end
Main function
#import<Foundation/Foundation.h>#import "CashContext.h"#import "CashSuper.h"#import "CashNormal.h"#import "CashRebate.h"#import "CashReturn.h"intMainintargcConst Char*argv[]) {@autoreleasepool {CharType='B'; Doubleresult=0.0; Cashcontext(c!=Nil; Switch(type) { Case 'N': CC=[[Cashcontext Alloc]initwith:[[cashnormal Alloc]init]]; Break; Case 'B': CC=[[cashcontext alloc]initwith:[[cashrebate alloc]initwithrebate:0.7]]; Break; Case 'T': CC=[[cashcontext Alloc]initwith:[[cashreturn Alloc] Initwithmoneycondition: -: -]]; Break; } result= [CC GetResult: -]; NSLog (@"%f", result); } return 0;}
Run Discount Classes
Strategy mode of big talk design pattern