阅读 241

ios-策略模式

  • 定义一个协议
HCDCashBase.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@protocol HCDCashBase <NSObject>
//@property(nonatomic,assign)CGFloat money;
-(CGFloat)acceptCash:(CGFloat)cash;
@end
复制代码
  • normal策略的实现
#import <Foundation/Foundation.h>
#import "HCDCashBase.h"
@interface HCDCashNormal : NSObject<HCDCashBase>

@end

复制代码
#import "HCDCashNormal.h"

@implementation HCDCashNormal

//@synthesize money = _money;

-(CGFloat)acceptCash:(CGFloat)cash{
    return cash;
}
@end

复制代码
  • robate策略的实现
#import <Foundation/Foundation.h>
#import "HCDCashBase.h"
@interface HCDCashRobate : NSObject<HCDCashBase>
-(instancetype)initWithMoneyRebate:(CGFloat)moneyRebate;
@end

复制代码
#import "HCDCashRobate.h"

@interface HCDCashRobate ()
@property(nonatomic)CGFloat moneyRebate;
@end

@implementation HCDCashRobate
//@synthesize money = _money;
-(instancetype)initWithMoneyRebate:(CGFloat)moneyRebate{
    self = [super init];
    if (self) {
        _moneyRebate = moneyRebate;
    }
    return self;
}

-(CGFloat)acceptCash:(CGFloat)cash{
    return self.moneyRebate * cash;
}

@end

复制代码
  • return策略的实现
#import <Foundation/Foundation.h>
#import "HCDCashBase.h"
@interface HCDCaseReturn : NSObject<HCDCashBase>
-(instancetype)initWithMoneyReturn:(CGFloat)moneyReturn;
@end

复制代码
#import "HCDCaseReturn.h"

@interface HCDCaseReturn ()

@property(nonatomic)CGFloat moneyReturn;
@end

@implementation HCDCaseReturn
//@synthesize money = _money;

-(instancetype)initWithMoneyReturn:(CGFloat)moneyReturn{
    self = [super init];
    if (self) {
        _moneyReturn = moneyReturn;
    }
    return self;
}
-(CGFloat)acceptCash:(CGFloat)cash{
    return cash - self.moneyReturn;
}
@end
复制代码
  • 定义一个枚举对外的一个统一接口
#import <Foundation/Foundation.h>
#import "HCDCashBase.h"


typedef NS_ENUM(NSInteger, HCDCashType){
    CashTypeNormal = 0,
    CashTypeRobate,
    CashTypeReturn
};


@interface HCDCashContext : NSObject
//-(instancetype)initWithCashSuper:(id<HCDCashBase>)cashBase;
-(instancetype)initWithCashType:(HCDCashType)type;
-(CGFloat)getResult:(CGFloat)money;
@end

复制代码
#import "HCDCashContext.h"
#import "HCDCaseReturn.h"
#import "HCDCashNormal.h"
#import "HCDCashRobate.h"
@interface HCDCashContext()
@property(strong,nonatomic) id<HCDCashBase> cashSuper;
@end

@implementation HCDCashContext
//-(instancetype)initWithCashSuper:(id<HCDCashBase>)cashBase{
//    self = [super init];
//    if (self) {
//        self.cashSuper = cashBase;
//    }
//    return self;
//}
//当传入不同的枚举时就会调用不同的算法
-(instancetype)initWithCashType:(HCDCashType)type{
    self = [super init];
    if (self) {
        if (type == CashTypeNormal) {
            self.cashSuper = [[HCDCashNormal alloc]init];
           // self.cashSuper.money = money;
        }else if(type == CashTypeRobate){
            self.cashSuper = [[HCDCashRobate alloc]initWithMoneyRebate:0.8];
           // self.cashSuper.money = money;
        }else if(type == CashTypeReturn){
            self.cashSuper = [[HCDCaseReturn alloc]initWithMoneyReturn:5];
        }
    }
    return self;
}

-(CGFloat)getResult:(CGFloat)money{
    return [self.cashSuper acceptCash:money];
}
@end
复制代码
  • 调用 根据传入的不同策略后返回不一样的结果
    HCDCashContext *context = [[HCDCashContext alloc]initWithCashType:CashTypeNormal];
    NSLog(@"结果是%f",[context getResult:100]);

    
    HCDCashContext *contextReturn = [[HCDCashContext alloc]initWithCashType:CashTypeReturn];
    NSLog(@"结果是%f",[contextReturn getResult:100]);
    
    HCDCashContext *contextRobate = [[HCDCashContext alloc]initWithCashType:CashTypeRobate];
    NSLog(@"结果是%f",[contextRobate getResult:100]);

复制代码
  • 总结其实就是 利用不同的枚举做不同的事情,只是我们不在关注里面具体怎么做了。

参考文章

文章分类
iOS
文章标签