策略

设计模式 / 行为型模式
image

//Strategy
@protocol Strategy <NSObject>
- (NSInteger)doOperation:(NSInteger)a b:(NSInteger)b;
@end

//ConcreteStrategy
@interface AddStrategy : NSObject <Strategy>
@end

@implementation AddStrategy
- (NSInteger)doOperation:(NSInteger)a b:(NSInteger)b {
    return a + b;
}
@end

//ConcreteStrategy
@interface SubtractStrategy : NSObject <Strategy>
@end

@implementation SubtractStrategy
- (NSInteger)doOperation:(NSInteger)a b:(NSInteger)b {
    return a - b;
}
@end

//Context
@interface Calc : NSObject
@property (nonatomic, strong) id<Strategy> strategy;

- (NSInteger)execute:(NSInteger)a b:(NSInteger)b;
@end

@implementation Calc

- (NSInteger)execute:(NSInteger)a b:(NSInteger)b {
    if (self.strategy == nil) {
        return 0;
    }
    return [self.strategy doOperation:a b:b];
}
@end

//Client
Calc *calc = [[Calc alloc] init];
NSInteger result1 = [calc execute:5 b:3];
//result1 is 0

calc.strategy = [[AddStrategy alloc] init];
NSInteger result2 = [calc execute:5 b:3];
//result2 is 8

calc.strategy = [[SubtractStrategy alloc] init];
NSInteger result3 = [calc execute:5 b:3];
//result3 is 2

NSLog(@"result1 is %ld", (long)result1);
NSLog(@"result2 is %ld", (long)result2);
NSLog(@"result3 is %ld", (long)result3);


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Read more

问题。商店计算运费:按重量、按距离或按固定价格。如果一个方法用一连串条件包含所有方式,每增加一种方式它就膨胀一次。修改一种方式可能破坏其他方式,而且任何一种方式都无法单独测试。

思路。完成工作的每种方式都变成一个单独的对象,称为策略。所有策略的形式相同:输入相同,结果的种类也相同。需要完成这项工作的对象称为上下文,它持有一个策略并调用它,却不知道自己持有的是哪一个。客户端代码选择策略,并可以随时替换它。

如何阅读示例。每个具体策略都是一种算法,它们都接收相同的输入。客户端代码选择一个策略。使用策略的代码只是调用它,并不知道自己收到的是哪一个。在大多数示例中,每换一次策略,同样的两个数就得出不同的结果。其中大多数示例还会针对没有策略的情况给出答案。在某些语言中,策略只是一个函数。

何时使用。当完成一项工作有多种方式、而选择取决于用户、数据或设置时,使用策略模式。只有一种不会变化的算法时不要使用它。策略模式与状态模式看起来相似,区别在于由谁做出选择。这里由客户端代码从外部选择算法,各个策略彼此一无所知。在状态模式中,对象自己从一个状态转到另一个状态。

常见错误。让上下文检查自己持有的是哪个策略。这样条件判断又回来了,模式也就失去了意义。上下文只应调用策略。