策略模式定义:定义了算法家族,分别封装起来,让他们之前可以互相转换,此模式让算法的变化,不会影响到使用算法的客户。
优点:
1、策略模式提供了对“开闭原则”的完美支持,用户可以在不修改原有系统的基础上选择算法或行为,也可以灵活地增加新的算法或行为。
2、策略模式提供了可以替换继承关系的办法。
缺点:
1、客户端必须知道所有的策略类,并自行决定使用哪一个策略类。
2、策略模式将造成产生很多策略类。
项目地址:https://github.com/GameRisker/Study-Design-Patterns.git
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
// // main.m // Strategy // // Created by GameRisker on 15/12/13. // Copyright (c) 2015年 GameRisker. All rights reserved. // #import "CashContext.h" #import <Foundation/Foundation.h> int main(int argc, const char *argv[]) { @autoreleasepool { CashContext *cash1 = [[CashContext alloc] initWithType:CashType_Normal]; NSLog(@"normal is : %lf", [cash1 GetResult:1000]); CashContext *cash2 = [[CashContext alloc] initWithType:CashType_Rebate]; NSLog(@"rebate is : %lf", [cash2 GetResult:1000]); CashContext *cash3 = [[CashContext alloc] initWithType:CashType_Return]; NSLog(@"returnMoney is : %lf", [cash3 GetResult:1000]); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// // CashSuper.h // // // Created by GameRisker on 15/12/13. // // #import <Foundation/Foundation.h> @interface CashSuper : NSObject - (double)accpetCash:(double)cash; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // CashSuper.m // // // Created by GameRisker on 15/12/13. // // #import "CashSuper.h" @implementation CashSuper - (double)accpetCash:(double)cash { return 0; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // CashReturn.h // // // Created by GameRisker on 15/12/13. // // #import "CashSuper.h" #import <Foundation/Foundation.h> @interface CashReturn : CashSuper @property(nonatomic) double m_money; @property(nonatomic) double m_returnMoney; - (id)initWithCondition:(double)money returnMoney:(double)returnmoney; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// // CashReturn.m // // // Created by GameRisker on 15/12/13. // // #import "CashReturn.h" @implementation CashReturn - (id)initWithCondition:(double)money returnMoney:(double)returnmoney { if (self = [super init]) { self.m_money = money; self.m_returnMoney = returnmoney; } return self; } - (double)accpetCash:(double)cash { int num = (int)floor(cash / self.m_money); return cash - num * self.m_returnMoney; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// // CashRebate.h // // // Created by GameRisker on 15/12/13. // // #import "CashSuper.h" @interface CashRebate : CashSuper @property(nonatomic) double m_rebate; - (id)initWithRebate:(double)rebate; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
// // CashRebate.m // // // Created by GameRisker on 15/12/13. // // #import "CashRebate.h" @implementation CashRebate - (id)initWithRebate:(double)rebate { if (self = [super init]) { self.m_rebate = rebate; } return self; } - (double)accpetCash:(double)cash { return self.m_rebate * cash; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// // CashNormal.h // // // Created by GameRisker on 15/12/13. // // #import "CashSuper.h" @interface CashNormal : CashSuper @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// // CashNormal.m // // // Created by GameRisker on 15/12/13. // // #import "CashNormal.h" @implementation CashNormal - (double)accpetCash:(double)cash { return cash; } @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
// // CashContext.h // // // Created by GameRisker on 15/12/13. // // #import "CashNormal.h" #import "CashRebate.h" #import "CashReturn.h" #import "CashSuper.h" #import <Foundation/Foundation.h> @interface CashContext : NSObject typedef NS_ENUM(NSInteger, CashType) { CashType_Normal, CashType_Rebate, CashType_Return, }; @property(readwrite) CashSuper *m_cashsuper; - (id)initWithType:(CashType)type; - (double)GetResult:(double)money; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
// // CashContext.m // // // Created by GameRisker on 15/12/13. // // // // // #import "CashContext.h" @implementation CashContext - (id)initWithType:(CashType)type { if (self = [super init]) { switch (type) { case CashType_Rebate: self.m_cashsuper = [[CashRebate alloc] initWithRebate:0.8]; break; case CashType_Return: self.m_cashsuper = [[CashReturn alloc] initWithCondition:300 returnMoney:100]; break; case CashType_Normal: default: self.m_cashsuper = [[CashNormal alloc] init]; break; } } return self; } - (double)GetResult:(double)money { return [self.m_cashsuper accpetCash:money]; } @end |
参考:http://www.cnblogs.com/chenssy/(Java 版本)