简体   繁体   English

使用清洗委托模式

[英]Using cleaning delegate pattern

I have a protocol like this : 我有这样的协议:

#import <Foundation/Foundation.h>

@protocol StoreDisplayerDelegate <NSObject>

-(void) changeActionForObjectWithId:(NSString *)objectID ;

@end

and i have a callass with conforms to the precedent protocol StoreDisplayerDelegate 而且我有一个符合先前协议StoreDisplayerDelegate

@interface ShelfVC : UIViewController :<StoreDisplayerDelegate>
....

@implementation ShelfVC 
...

- (void)viewDidLoad {

   ...
   DownloadManager *manager = [DownloadManager sharedInstance];
   [manager setStoreDisplayerDelegate:self];
   ....
}

#pragma mark StoreDisplayerDelegate methods
-(void) changeActionForObjectWithId:(NSString *)objectID {
   ......
}

@end

And in my code ( in the same class) sometimes i am calling the delegate methods to do something form me, for example : 并且在我的代码中(在同一类中)有时我会调用委托方法来完成某些形式的工作,例如:

- (void)anOtherMethod{
   [self changeActionForObjectWithId:nil];
}

My Questions 1. is : When object is a delegate for an other object, is the methods implemented by the delegate called only by the other object ( witch have a reference for it ) ? 我的问题1.是:当object是另一个对象的委托时,该委托实现的方法是否仅由另一个对象调用(witch有其引用)? i mean by this, for example in the code i have shown should the methode changeActionForObjectWithId: just called by the downLoad manager or can i use it in the inernal of my class like this : 我的意思是,例如,在我显示的代码中,方法changeActionForObjectWithId:应该由changeActionForObjectWithId:管理器调用,还是可以在我的类的内部使用,例如:

  1. is what i am doing cleaning or bad design of using Delegate pattern ? 我正在做清洁或使用代理模式的不良设计是什么?

I hope that it is clear. 我希望这很清楚。

Your delegate method name sounds like a command. 您的委托方法名称听起来像一条命令。

-(void)changeActionForObjectWithId:(NSString *)objectID;

It sounds like your StoreDisplayer is telling delegate to do something. 听起来您的StoreDisplayer告诉委托人做某事。 The fact that you are also tempted to call that method from within the ViewController confirms it. 您也很想从ViewController中调用该方法,这一事实证实了这一点。

That is not the delegate pattern. 那不是委托模式。 The delegate pattern is for a class to inform a delegate of a change, or to ask the delegate for some information. 委托模式用于类,以将更改通知给委托人,或向委托人询问一些信息。 The delegating class (StoreDisplayer?) shouldn't know about what any particular delegate does, so it shouldn't be able to give it direct specific commands. 委托类(StoreDisplayer?)不应该知道任何特定的委托所做的事情,因此它不能直接向其提供特定的命令。 Only delegate generic behaviour to it. 仅将一般行为委派给它。

Delegate method look more like these examples: 委托方法看起来更像这些示例:

-(BOOL)actionShouldChangeForStoreDisplayer:(StoreDisplayer*)storeDisplayer;
-(void)actionWillChangeForStoreDisplayer:(StoreDisplayer*)storeDisplayer objectId:(NSString *)objectId;
-(void)actionDidChangeForStoreDisplayer:(StoreDisplayer*)storeDisplayer objectId:(NSString *)objectId;

I'm not saying those are what you need, but they should give you the idea. 我并不是说这些就是您所需要的,但它们应该可以给您这个想法。

When your delegate methods look like this, clearly you will not be tempted to call them from anything other than the class that's doing the delegation (StoreDisplayer). 当您的委托方法看起来像这样时,显然,您将不会试图从进行委托的类(StoreDisplayer)之外的任何其他方法调用它们。

In my project I have the same situation for different purpose. 在我的项目中,出于不同的目的,我有相同的情况。 In my opinion the delegate method must be called only by the other object, because it is its method. 我认为委托方法只能由另一个对象调用,因为它是它的方法。

If you need to do certain action in the delegate method, its better to create another private method to performing the action and call it from the delegate methods. 如果您需要在委托方法中执行某些操作,最好创建另一个私有方法来执行该操作并从委托方法中调用它。

Some example. 一些例子。 Instead of doing this: 而不是这样做:

- (void)anOtherMethod {
   [self changeActionForObjectWithId:nil];
}

- (void)changeActionForObjectWithId:(NSString *)objectID {
   < some actions >
} 

I do this: 我这样做:

- (void)privateMethod{
   < some actions >
}

- (void)anOtherMethod {
   [self privateMethod];
}

- (void)changeActionForObjectWithId:(NSString *)objectID {
   [self privateMethod];
} 

Why do this? 为什么这样 Because you have to think to the delegate methods to an "extension" to your base object: if you delete the "changeActionForObjectWithId" (becuase, for example, you don't need the delegate anymore after a refactoring) the code will continue to work. 因为您必须考虑委托方法对基础对象的“扩展”:如果删除“ changeActionForObjectWithId”(例如,由于重构后不再需要委托),代码将继续工作。

A delegate is a protocol that allows an object to perform certain actions. 委托是一种协议,它允许对象执行某些操作。

In your example: 在您的示例中:

@interface ShelfVC : UIViewController : <StoreDisplayerDelegate>

This tells the compiler that UIViewController is going to implement some methods in the StoreDisplayerDelegate protocol. 这告诉编译器UIViewController将在StoreDisplayerDelegate协议中实现某些方法。 How the object that applicable to the StoreDisplayerDelegate behave would depend on the protocol in the delegate methods. 适用于StoreDisplayerDelegate的对象的行为方式将取决于委托方法中的协议。

You got a little confused with delegates and protocols. 您对委托和协议感到有些困惑。 a delegate is a design-pattern wich uses a protocol. 委托是一种使用协议的设计模式。 a protocol only defines methods and properties expected to be implemented by another (unspecific) class. 协议仅定义预期由另一个(非特定)类实现的方法和属性。 from where this methods/properties get accessed doesn't matter. 从何处访问此方法/属性无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM