Мне нужно разработать шаблон стратегии, где у меня есть основной класс с другими тремя классами, где мне нужно ссылаться на объекты других трех классов, используя основной объект класса. Чтобы решить эту проблему, мне поможет шаблон стратегии? Если да, пожалуйста, дайте мне синтаксис в Objective-C?
Как создать шаблон стратегии в Objective-C?
Ответ 1
Вам нужно посмотреть на механизм Objective-C protocol. Здесь простой протокол с одним требуемым методом:
@protocol Strategy <NSObject>
@required
- (void) execute;
@end
Затем вы объявляете класс, который выполняет этот протокол:
@interface ConcreteStrategyA : NSObject <Strategy>
{
// ivars for A
}
@end
Реализация должна предоставить метод -execute
(поскольку он был объявлен как @required
):
@implementation ConcreteStrategyA
- (void) execute
{
NSLog(@"Called ConcreteStrategyA execute method");
}
@end
Вы можете создать аналогичный класс ConcreteStrategyB
, но я не буду показывать его здесь.
Наконец, создайте класс контекста со свойством, поддерживающим текущую стратегию.
@interface Context : NSObject
{
id<Strategy> strategy;
}
@property (assign) id<Strategy> strategy;
- (void) execute;
@end
Вот реализация. Метод, который делегирует метод стратегии -execute
, просто называется также -execute, но он не обязательно должен быть.
@implementation Context
@synthesize strategy;
- (void) execute
{
[strategy execute];
}
@end
Теперь я сделаю несколько экземпляров и поставлю их для использования:
ConcreteStrategyA * concreteStrategyA = [[[ConcreteStrategyA alloc] init] autorelease];
ConcreteStrategyB * concreteStrategyB = [[[ConcreteStrategyB alloc] init] autorelease];
Context * context = [[[Context alloc] init] autorelease];
[context setStrategy:concreteStrategyA];
[context execute];
[context setStrategy:concreteStrategyB];
[context execute];
Консольный вывод показывает, что стратегия была успешно изменена:
2010-02-09 19:32:56.582 Strategy[375:a0f] Called ConcreteStrategyA execute method
2010-02-09 19:32:56.584 Strategy[375:a0f] Called ConcreteStrategyB execute method
Обратите внимание, что если протокол не указывает @required
, метод не является обязательным. В этом случае контекст должен проверить, реализует ли стратегия метод:
- (void) execute
{
if ([strategy respondsToSelector:@selector(execute)])
[strategy execute];
}
Это обычный шаблон Cocoa, называемый delegation. Для получения дополнительной информации о делегировании и других шаблонах проектирования в Cocoa, см. Это.
Ответ 2
Вот немного более конкретный пример. Вы можете поместить каждый элемент в отдельный файл. Я поставил все это в одном файле для удобства понимания.
// main.m
// StrategyWikipediaExample
//
// Created by steve on 2014-07-08.
// Copyright (c) 2014 steve. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
Equivalent to Java Interface
All concrete Strategies conform to this protocol
*/
@protocol MathOperationsStrategy<NSObject>
- (void)performAlgorithmWithFirstNumber:(NSInteger)first secondNumber:(NSInteger)second;
@end
/**
Concrete Strategies.
Java would say they "Extend" the interface.
*/
@interface AddStrategy : NSObject<MathOperationsStrategy>
@end
@implementation AddStrategy
- (void)performAlgorithmWithFirstNumber:(NSInteger)first secondNumber:(NSInteger)second
{
NSInteger result = first + second;
NSLog(@"Adding firstNumber: %ld with secondNumber: %ld yields : %ld", first, second, result);
}
@end
@interface SubtractStrategy : NSObject<MathOperationsStrategy>
@end
@implementation SubtractStrategy
- (void)performAlgorithmWithFirstNumber:(NSInteger)first secondNumber:(NSInteger)second
{
NSInteger result = first - second;
NSLog(@"Subtracting firstNumer: %ld with secondNumber: %ld yields: %ld", first, second, result);
}
@end
@interface MultiplyStrategy : NSObject<MathOperationsStrategy>
@end
@implementation MultiplyStrategy
- (void)performAlgorithmWithFirstNumber:(NSInteger)first secondNumber:(NSInteger)second
{
NSInteger result = first * second;
NSLog(@"Multiplying firstNumber: %ld with secondNumber: %ld yields: %ld", first, second, result);
}
@end
@interface Context : NSObject
@property (weak, nonatomic)id<MathOperationsStrategy>strategy; // reference to concrete strategy via protocol
- (id)initWithMathOperationStrategy:(id<MathOperationsStrategy>)strategy; // setter
- (void)executeWithFirstNumber:(NSInteger)first secondNumber:(NSInteger)second;
@end
@implementation Context
- (id)initWithMathOperationStrategy:(id<MathOperationsStrategy>)strategy
{
if (self = [super init]) {
_strategy = strategy;
}
return self;
}
- (void)executeWithFirstNumber:(NSInteger)first secondNumber:(NSInteger)second
{
[self.strategy performAlgorithmWithFirstNumber:first secondNumber:second];
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
id<MathOperationsStrategy>addStrategy = [AddStrategy new];
Context *contextWithAdd = [[Context alloc] initWithMathOperationStrategy:addStrategy];
[contextWithAdd executeWithFirstNumber:10 secondNumber:10];
}
return 0;
}