Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a strategy pattern in Objective-C?

I need to develop a strategy pattern where i have a main class with other three classes where i need to refer to the objects of the other three classes using the main class object.To solve this is the strategy pattern will help me? If so please do give me the syntax in Objective-C?

like image 294
Cathy Avatar asked Feb 10 '10 03:02

Cathy


People also ask

How do you implement a Strategy pattern?

In Strategy pattern, we create objects which represent various strategies and a context object whose behavior varies as per its strategy object. The strategy object changes the executing algorithm of the context object.

What are the objectives of strategic pattern?

The goal of the strategy design pattern is to allow the Client to perform the core algorithm, based on the locally-selected Strategy . In so doing, this allows different objects or data to use different strategies, independently of one another.

What is strategy design pattern with example?

Strategy design pattern is one of the behavioral design pattern. Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.

What problem does Strategy pattern solve?

Strategy Pattern: Problems It Solves Strategy Pattern prevents hard-wiring of all the algorithms into the program. This makes our program complex and much more bogus and hard to refactor/maintain and understand. This, in turn, makes our program to contain algorithms they do not use.


1 Answers

You'll want to look at Objective-C's protocol mechanism. Here's a simple protocol with a single required method:

@protocol Strategy <NSObject>

@required
- (void) execute;

@end

Then you declare a class that fulfills that protocol:

@interface ConcreteStrategyA : NSObject <Strategy>
{
    // ivars for A
}
@end

The implementation must provide the -execute method (since it was declared as @required):

@implementation ConcreteStrategyA

- (void) execute
{
    NSLog(@"Called ConcreteStrategyA execute method");
}

@end

You can make a similar ConcreteStrategyB class, but I'm not going to show it here.

Finally, make a context class with a property maintaining the current strategy.

@interface Context : NSObject
{
    id<Strategy> strategy;
}
@property (assign) id<Strategy> strategy;

- (void) execute;

@end

Here is the implementation. The method that delegates to the strategy's -execute method just happens to be called -execute as well, but it doesn't have to be.

@implementation Context

@synthesize strategy;

- (void) execute
{
    [strategy execute];
}

@end

Now I'll make a few instances and put them to use:

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];    

The console output shows that the strategy was successfully changed:

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

Note that if the protocol does not specify @required, the method is optional. In this case, the context needs to check whether the strategy implements the method:

- (void) execute
{
    if ([strategy respondsToSelector:@selector(execute)])
        [strategy execute];
}

This is a common Cocoa pattern called delegation. For more information on delegation and other design patterns in Cocoa, see this.

like image 128
Jon Reid Avatar answered Oct 11 '22 12:10

Jon Reid