好久了,过去了一个月没有动笔写东西了,除了工作的忙,还有最近在学习一个课程和看一些技术类的书籍,腾不出时间来写博客了,说了这么多,其实归根结底都是我懒的借口,确实挺惭愧,还是得继续加油,多写多练习,做更好的自己。。。
策略模式
本文使用OC语言实现策略模式的实现
定义
定义一系列可以相互替换的算法类,提供给客户端相同的调用接口,客户端调用不同的对象的相同方法来达到快速切换算法的目的。
使用场景
下面是从编程原则讨论策略模式的使用场景
- 针对同一个问题的多种不同的处理方式,但是具体的实现有差异,需要实现统一的接口,对变化部分进行封装【面向抽象而非面向具体原则】。
- 分支太多导致了客户端对算法实现类的依赖太大,算法扩展需要去修改实现--添加对应的分支,使得扩展不方便以及算法类中的代码量膨胀。为了让算法类的扩展容易,需要对具体的算法独立封装【开闭原则】;为了让算法代码保持最小和最简单,需要对具体的算法独立实现【单一职责原则】。
案例实现
理财产品的回报计算,处理方式是一样的,客户端只要给固定的参数(平台、月份、金额),需要计算出本金及利息的和,不同理财产品的计算接口定义是统一的,在实现上有差别,所以类似这种场景是比较适合使用策略模式的。
不同产品的利率:
有利网:
短期理财:6个月以内,年化收益:3%
中期理财:12个月以内,年化收益:4%
长期理财:24个月以内,年化收益:4.5%
支付宝:
定期理财3个月,年化收益:7%
定期理财6个月,年化收益:8%
定期理财12个月,年化收益:9.5%
定期理财24个月,年化收益:10.5% :10.5%
下面是理财产品这个案例的具体模块的代码实现。
定义策略接口
这个案例中策略接口是理财产品计算算法的接口
#import <Foundation/Foundation.h>
@protocol FinancyStrategyProtocal <NSObject>
- (NSInteger)financyWithMonth:(NSInteger)month money:(NSInteger)money;
@end
定义Context类
Context类使用到了算法对象,这个对象是可以相互替换的
// FinancyContext.h
#import <Foundation/Foundation.h>
#import "FinancyStrategyProtocal.h"
@interface FinancyContext : NSObject
@property (nonatomic, strong) id<FinancyStrategyProtocal> financy;
- (instancetype)initWithFinancy:(id<FinancyStrategyProtocal>)financy;
- (NSInteger)financyWithMonth:(NSInteger)month money:(NSInteger)money;
@end
// FinancyContext.m
#import "FinancyContext.h"
@implementation FinancyContext
- (instancetype)initWithFinancy:(id<FinancyStrategyProtocal>)financy {
self = [super init];
if (self) {
_financy = financy;
}
return self;
}
- (NSInteger)financyWithMonth:(NSInteger)month money:(NSInteger)money {
return [_financy financyWithMonth:month money:money];
}
@end
策略类的具体实现
A. 有利网的算法策略实现
// YouLiFinancyStrategy.h
#import <Foundation/Foundation.h>
#import "FinancyStrategyProtocal.h"
@interface YouLiFinancyStrategy : NSObject <FinancyStrategyProtocal>
@end
// YouLiFinancyStrategy.m
#import "YouLiFinancyStrategy.h"
@implementation YouLiFinancyStrategy
- (NSInteger)financyWithMonth:(NSInteger)month money:(NSInteger)money {
// 短期理财:6个月以内,年化收益:3%
// 中期理财:12个月以内,年化收益:4%
// 长期理财:24个月以内,年化收益:4.5%
if (month <= 6) {
return money * 0.03f / 12 * month + money;
} else if (month <= 12) {
return money * 0.04f / 12 * month + money;
} else if (month <= 24) {
return money * 0.045f / 12 * month + money;
}
return 0;
}
@end
A. 余额宝的算法策略实现
// AlipayFinancyStrategy.h
#import <Foundation/Foundation.h>
#import "FinancyStrategyProtocal.h"
@interface AlipayFinancyStrategy : NSObject <FinancyStrategyProtocal>
@end
// AlipayFinancyStrategy.m
#import "AlipayFinancyStrategy.h"
@implementation AlipayFinancyStrategy
- (NSInteger)financyWithMonth:(NSInteger)month money:(NSInteger)money {
// 定期理财3个月,年化收益:7%
// 定期理财6个月,年化收益:8%
// 定期理财12个月,年化收益:9.5%
// 定期理财24个月,年化收益:10.5%
if (month <= 3) {
return money * 0.07f / 12 * month + money;
} else if (month <= 6) {
return money * 0.08f / 12 * month + money;
} else if (month <= 12) {
return money * 0.095f / 12 * month + money;
} else if (month <= 24) {
return money * 0.105f / 12 * month + money;
}
return 0;
}
@end
客户端使用
id<FinancyStrategyProtocal> alipayFinancy = [[AlipayFinancyStrategy alloc] init];
FinancyContext* context = [[FinancyContext alloc] initWithFinancy:alipayFinancy];
NSInteger money = [context financyWithMonth:6 money:10000];
NSLog(@"Alipay money = %@", @(money));
id<FinancyStrategyProtocal> ylFinancy = [[YouLiFinancyStrategy alloc] init];
context.financy = ylFinancy;
money = [context financyWithMonth:6 money:10000];
NSLog(@"YouLi money = %@", @(money));
UML分析

网友评论