2010-02-10 13 views
12

मुझे एक रणनीति पैटर्न विकसित करने की आवश्यकता है जहां मेरे पास अन्य तीन वर्गों के साथ एक मुख्य वर्ग है जहां मुझे मुख्य वर्ग वस्तु का उपयोग करके अन्य तीन वर्गों की वस्तुओं को संदर्भित करने की आवश्यकता है। हल करने के लिए रणनीति पैटर्न मेरी मदद करेगा? यदि ऐसा है तो कृपया मुझे उद्देश्य-सी में वाक्यविन्यास दें?उद्देश्य-सी में एक रणनीति पैटर्न कैसे बनाएं?

+0

यह आपके मौजूदा सवाल का डुप्लिकेट हो गया लगता है: http://stackoverflow.com/questions/2229026 –

+0

हाँ लेकिन मैं अपने पिछले प्रश्न – Cathy

उत्तर

39

आप उद्देश्य-सी के protocol तंत्र को देखना चाहते हैं।

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

कार्यान्वयन -execute विधि प्रदान करना होगा (क्योंकि यह @required के रूप में घोषित किया गया था):

@protocol Strategy <NSObject> 

@required 
- (void) execute; 

@end 

तो फिर तुम एक वर्ग है कि प्रोटोकॉल को पूरा घोषित: यहाँ एक भी आवश्यक विधि के साथ एक सरल प्रोटोकॉल है:

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

यह एक आम कोको पैटर्न delegation कहा जाता है। कोको में प्रतिनिधिमंडल और अन्य डिजाइन पैटर्न के बारे में अधिक जानकारी के लिए, see this

+0

में रणनीति पैटर्न के बारे में कुछ भी उल्लेख नहीं किया है यू के लिए बहुत बहुत धन्यवाद उत्तर दें, एक संदेह यह है कि 'setStrategy' कीवर्ड यहां आपके द्वारा किए गए उदाहरणों में क्या दर्शाता है। – Cathy

+0

'-setStrategy: 'विधि स्वचालित रूप से' @ संश्लेषण' निर्देश द्वारा उत्पन्न होती है। अगर संपत्ति को 'बनाए रखा' घोषित किया जाता है, तो यह बनाए रखने/रिलीज करने का ख्याल रखता है (लेकिन आपको अभी भी इसे '-dealloc' में रिलीज़ करना होगा)। लेकिन अगर संपत्ति 'असाइन' घोषित की जाती है, तो यह एक कमजोर संदर्भ के रूप में एक सरल असाइनमेंट करता है, इस धारणा के साथ कि निर्दिष्ट आवृत्ति मौजूद है और कहीं और प्रबंधित की जाती है। उस स्थिति में इसे जारी नहीं किया जाना चाहिए। ... वास्तव में, मैं अपना दूसरा जवाब इस दूसरे रूप में बदलने जा रहा हूं। –

+0

उत्तर के लिए धन्यवाद।@protocol मुझे इसे अलग फ़ाइल – Cathy

1

यहां एक ठोस उदाहरण का थोड़ा और अधिक है। आप प्रत्येक आइटम को एक अलग फ़ाइल में डाल सकते हैं। मैंने समझ में आसानी के लिए इसे एक फाइल में रखा है।

// 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; 
} 
संबंधित मुद्दे