3

シングルトンと委任に関する十分な情報を読みました。だから、私はシングルトンとは何かを理解していると思います。委任について私はまだ混乱しています。委任の概念は理解していますが、委任を理解するためのプロトコルを作成する必要があります。

OK、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
4

1 に答える 1

9

シングルトンの考え方は、アプリ全体がこの1つのクラスにアクセスできるということです。複数のViewControllerには、データベースからのデータが必要な場合があります。あなたの場合、私はあなたのfetchDataメソッドを変更します(そして、今は実際には慣例に従わないので、おそらくその名前を変更します):

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

デリゲートは1対1の通信を目的としています。つまり、1つのオブジェクトにデリゲートがあり、その1つの特定のデリゲートにメッセージを送信します。

これは、シングルトンと委任がうまく連携しないことを意味します。シングルトンは複数の受信者にメッセージを送信するように作られていますが、委任パターンは1対1の通信を目的としています。したがって、2つのオプションがあります。シングルトンを使用せずに委任パターンを使用するか、シングルトンを使用NSNotificationCenterしてオブザーバーに変更を通知するために使用することができます。

于 2013-02-06T17:32:29.220 に答える