iOS App设计模式开发中策略模式的实现示例

2020-01-14 22:21:13于丽
复制代码

 

#import <Foundation/Foundation.h>
#import "CashSuper.h"

@interface CashContext :NSObject{
@private CashSuper *cs;
}
-(CashContext*)MyInit:(int)Types;
-(void)SetCashSuper:(CashSuper*)cashSuper;
-(double)GetResult:(double)money;
@end

 


CashContext类实现

 

 

复制代码

 

#import "CashContext.h"
#import "CashNormal.h"
#import "CashRebate.h"
#import "CashReturn.h"

@implementation CashContext

-(CashContext*)MyInit:(int)Types{
    int myTypes;
    myTypes = Types;
    switch(myTypes) {
        case 1:
            [self SetCashSuper:[[CashNormalalloc]init]];
            break;
        case 2:
            [self SetCashSuper:[[CashReturnalloc]MyInit:300 And:100]];
            break;
        case 3:
            [self SetCashSuper:[[CashRebatealloc]MyInit:0.8]];
            break;
        default:
            break;
    }
    return self;
}
-(void)SetCashSuper:(CashSuper*)cashSuper{
    cs = cashSuper;
}
-(double)GetResult:(double)money{
    return [cs AcceptCash:money];
}

@end

 

 


CashSuper类接口

 

复制代码

 

#import <Foundation/Foundation.h>