2013-02-06 50 views
3

我讀了關於單身人士和代表團的足夠信息。所以,我想我明白什麼是單身。關於代表團我仍然混淆。我理解代表團的概念,但我需要爲理解代表團創建我的協議。單身人士和代表團

好吧,我創建單身人士與我的實體從CoreData工作。也許我錯了,它不是單身人士,請告訴我關於它。我的單身人士是FetchData。

Fetchdata.h

#import <Foundation/Foundation.h> 

@interface FetchData : NSObject <UIApplicationDelegate> 

+(FetchData*) fetchData; 

-(NSArray*)fetchLogin:(NSString*)name; 
-(BOOL)newGroup:(NSString*)group forLogin:(NSString*)login; 
-(NSMutableArray*)contactsForGroup:(NSString*)group; 
-(BOOL)newContact:(NSString*)name surname:(NSString*)surname withDatas:(NSArray*)array; 
//other methods 

@end 

Fetchdata.m

#import "FetchData.h" 
#import "Contact.h" 
#import "Login.h" 
#import "Group.h" 
#import "AppDelegate.h" 

@interface FetchData() 
@property (nonatomic, strong) NSEntityDescription *loginEntity; 
@property (nonatomic, strong) NSEntityDescription* groupEntity; 
@property (nonatomic, strong) NSManagedObjectContext* context; 
@property (nonatomic, strong) NSEntityDescription* contactEntity; 
@property (nonatomic, strong) AppDelegate* appDelegate; 
//other properties 
@end 

@implementation FetchData 
@synthesize //my properties 

+(FetchData*) fetchData 
{ 
static FetchData* fetchData = nil; 
if (!fetchData) 
    fetchData = [[super allocWithZone:nil]init]; 
return fetchData; 
} 

+(id)allocWithZone:(NSZone *)zone 
{ 
return [self fetchData]; 
} 

//implementation my methods 
@end 

所以,它很容易與CoreData的人現在對我來說。我只需要進口FetchData,只是用於創建/刪除/修改/添加/排序方法...

SomeClass.m

#import "FetchData.h" 
#define fetching [FetchData fetchData] 

但我認爲,我可以用我的目標代表團。或者,與單身人士相比,這可能是最好的。所以我想重拍單身代表團。我需要這個問題的幫助。我應該做什麼?

如果我理解正確,我需要使用FetchData.h,FetchData.m中的所有方法創建協議,我可以不加修改地離開。而在SomeClass我需要導入FetchData並添加我的協議。像:

#import <Foundation/Foundation.h> 

@protocol FetchingDelegate 

//all methods from FetchData.h 

@end 

@interface FetchData : NSObject 
@property (nonatomic, strong) id <FetchingDelegate> delegate; 
@end 

FetchData.m

@interface FetchData() 
//all properties without changing 
@end 

@implementation FetchData 
@synthesize //all properties and delegate 

//implementation of methods 
@end 

SomeClass的

#import "FetchData.h" 

@interface SomeClass : NSObject <FetchingDelegate> 
@end 

@implementation SomeClass 

-(void)viewDidLoad 
{ 
    FetchData* fetching = [FetchData new] 
    fetching.delegate = self 
} 
//and now I can use any methods from protocol like [fetching anyMethod] 
//such I used with singleton 

回答

9

一個單身的想法是,你的整個應用程序可以訪問此一類。多個視圖控制器可能需要來自數據庫的數據。在你的情況,我會改變你的fetchData方法(可能更改其名稱,因爲它並沒有真正遵循慣例現在):

+(FetchData*) fetchData 
{ 
    static FetchData *fetchData; 
    dispatch_once_t token; 
    dispatch_once(&token, ^{ 
     if (!fetchData) 
      fetchData = [super init]; 
    } 
return fetchData; 
} 

代表都是爲了一個一對一的溝通,這意味着一個對象有代表並將任何消息發送給該特定的代表。

這意味着單身人士和代表團不會很好地合作。單身人士將消息發送給多個接收者,而委派模式則是爲了一對一溝通。因此,您有兩種選擇:您可以不使用單例並使用委派模式,也可以使用單例,並使用NSNotificationCenter來通知觀察者所做的更改。

+1

如果Delegate的需求很簡單,例如在後臺線程上運行的FetchData方法,您可以考慮使用Blocks而不是委託。 – Marc

+0

我寫的關於創建協議和委託 - 這是對還是錯的方向?我可以提出這樣的建議嗎? – Neznajka

+0

@Neznajka:我不確定你的意思。授權只是一個非常常用的協議實現。在這種情況下,創建一個沒有什麼意義,但是很多時候都是這樣。 –