第二章:商场促销——策略模式
策略模式的定义:
策略模式是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,知识实现不同,他可以以相同的方式调用所有的算法,减少了各类算法类与使用算法类之间的耦合
策略模式的优点 :
1. 策略模式的Strategy 类层次为Context定义了一些列的可供重用的算法或行为。继承有助于析取这些算法中的公共功能
2.简化了单元测试,因为每个算法都要自己的类,可以通过自己的接口单独测试
当不同的行为堆砌在一个类中时,就很难避免使用条件语句来选择合适的行为,将这些行为封装在一个个独立的Strategy类中,可以在使用这些行为的类中消除条件语句
策略模式就是用来封装算法的,但是在实践中,我们发现可以用它来封装几乎任何类型的规则,只要在分析过程中听到需要在不同实践应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性
在基本的策略模式中,选择所用具体实现的职责有客户端对象承担,并转给策略模式的Context对象
父类CashSuper
CashSuper.h
@interface CashSuper : NSObject -(double)acceptCash:(double)money; @end
CashSuper.m
@implementation CashSuper
-(double)acceptCash:(double)money{
return money;
}
@end
正常消费类 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
打折消费类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 "CashRebate.h" @implementation CashRebate - (instancetype)initWithRebate:(double)Rebate{ self = [super init]; if (self) { _Rebate=Rebate; } return self; } -(double)acceptCash:(double)money{ return money*_Rebate; } @end
返利消费类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" @implementation CashReturn -(instancetype)initWithMoneyCondition:(double)moneyCondition :(double)moneyReturn { self = [super init]; if (self) { _moneyCondition=moneyCondition; _moneyReturn=moneyReturn; } return self; } -(double)acceptCash:(double)money{ double result=money; if (money>=_moneyCondition) { result=money-((int)(money/_moneyCondition))*_moneyReturn; } return result; } @end
配置类,维护对对象的使用CashContext类
CashContext.h
#import <Foundation/Foundation.h> #import "CashSuper.h" @interface CashContext : NSObject @property(nonatomic,strong)CashSuper *cashsuper; -(instancetype)initWith:(CashSuper *)csuper; -(double)GetResult:(double)money; @end
CashContext.m
#import "CashContext.h" @implementation CashContext - (instancetype)initWith:(CashSuper *)csuper { self = [super init]; if (self) { _cashsuper=csuper; } return self; } -(double)GetResult:(double)money{ return [_cashsuper acceptCash:money]; } @end
主函数
#import <Foundation/Foundation.h> #import "CashContext.h" #import "CashSuper.h" #import "CashNormal.h" #import "CashRebate.h" #import "CashReturn.h" int main(int argc, const char * argv[]) { @autoreleasepool { char type='B'; double result=0.0; CashContext *cc=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:300 :100]]; break; } result = [cc GetResult:500]; NSLog(@"%f",result); } return 0; }
运行打折类