1

シングルトンパターンモデルを使用したいプロジェクトに取り組んでいます。このプロジェクトのデータモデルはシングルトンパターンを休ませたいです。私はこれに関するアップルのドキュメントを研究しています

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW6

http://www.oodesign.com/singleton-pattern.html

http://www.daveoncode.com/2011/12/19/fundamental-ios-design-patterns-sharedinstance-singleton-objective-c/

これで、カスタムオブジェクトクラスがオブジェクトの割り当てのメインルールを許可する必要があることがわかりましたが、このクラスオブジェクトの使用などの完全な実装が必要です。iphoneアプリ開発の初心者なので、この質問のどこかで間違っている場合はガイドしてください。

4

6 に答える 6

2

これを試して:

@implementation Singleton

+ (Singleton *)sharedInstance
{
     static Singleton *obj = nil;

    if (obj == nil)
        obj = [[self alloc] init];

    return obj;
}

@end
于 2012-07-13T12:04:19.467 に答える
1
static MyClass *_sharedInstance;

+ (MyClass *)sharedMyClass
{
    @synchronized([MyClass class]) {
        if (_sharedInstance == nil)
            [[self alloc] init];

        return _sharedInstance;
    }

    return nil;
}

+(id) alloc
{
    @synchronized([MyClass class]) {
        NSAssert(_sharedInstance == nil, @"Attempted to allocate a second instance of MyClass.");
        _sharedInstance = [super alloc];
        return _sharedInstance;
    }

    return nil;
}

+ (id) allocWithZone:(NSZone *)zone 
{
    @synchronized([MyClass class]) {
        NSAssert(_sharedInstance == nil, @"Attempted to allocate a second instance of MyClass.");
        _sharedInstance= [super allocWithZone:zone];
        return _sharedInstance;
    }
    return nil; //on subsequent allocation attempts return nil
}

- (id) copyWithZone:(NSZone *)zone 
{
    return self;
}

- (id)retain
{
    return self;
}

- (NSUInteger)retainCount
{
    return NSUIntegerMax;
}

- (oneway void)release
{
    // Do nothing
}

- (id)autorelease
{
    return self;
}
于 2012-07-13T12:06:08.943 に答える
1

iOS 4以降をターゲットにできる場合は、次のようにします。

//.h
+(MySingletonClass *)mySharedInstance;
-(void)doSomething;

//.m
+(MySingletonClass *)mySharedInstance {
    static dispatch_once_t pred;
    static MySingletonClass *shared = nil;

    dispatch_once(&pred, ^{
      shared = [[MySingletonClass alloc] init];
    });
    return shared;
}

-(void)doSomething
{
}

// override also the init if you want

アクセスするには、を実行して#import MySingletonClass.h、次のように好きな場所で使用します。

MySingletonClass* mySharedInstance = [MySingletonClass mySharedInstance];
[mySharedInstance doSomething];

このプロジェクトのデータモデルは、シングルトンパターンを休ませたいです。

私の経験に基づいて、私はシングルトンを乱用しませんでした。アプリケーションの保守が困難になる可能性があります。これを回避するには、データモデルをシングルトン内に配置します。データモデルに直接アクセスする(それらの周りにプロパティを作成する)かdoSomething、ラッパーとしてパブリックメソッド(たとえば)を使用することができます。

お役に立てれば。

于 2012-07-13T13:09:35.043 に答える
0

これは便利なリファレンスかもしれません:http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html

于 2012-07-13T12:05:55.793 に答える
0

通常、私はでオブジェクトを作成します

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

メソッドを作成し、そのオブジェクトに配置します。マクロを使用して、アプリの残りの部分で利用できるようにします。

#define APPLICATION ((AppDelegate*)([UIApplication sharedApplication].delegate))

アプリデリゲートの読み取り専用プロパティとして

モックを使用している場合のこのタイプのアプローチの利点は、非表示の静的オブジェクトではなく、単なる別のオブジェクトプロパティであるということです。

于 2012-07-13T13:02:05.137 に答える
0

私が使う:

#import <Foundation/Foundation.h>

@interface iCode_Framework : NSObject

@property (readonly, nonatomic) unsigned int iBufCapacity;
@property (readonly, nonatomic) unsigned int iPort;
@property (readonly, nonatomic) NSString * urlStr;

@end

#import "iCode_Framework.h"

static iCode_Framework * instance;

@implementation iCode_Framework

@dynamic iBufCapacity;
@dynamic iPort;
@dynamic urlStr;

- (unsigned int)iBufCapacity
{
    return 1024u;
};

- (unsigned int)iPort
{
    return 1978u;
};

- (NSString *)urlStr
{
    return @"localhost";
};

+ (void)initialize
{
    if (!instance) {
        instance = [[super allocWithZone:NULL] init];
    }
}

+ (id)allocWithZone:(NSZone * const)notUsed
{
    return instance;
}

@end

これは通常のクラスとまったく同じように使用され、allocとinitを呼び出します。allocとinitは入力に時間がかかるため、変数に割り当てて省略形を付けると便利なことがよくあります。例:

#import "iCode_FrameworkTests.h"
#import "iCode_Framework.h"

static iCode_Framework * c;

@implementation iCode_FrameworkTests

+ (void)initialize
{
    c  = [[iCode_Framework alloc] init];
}

- (void)setUp
{
    [super setUp];

    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.

    [super tearDown];
}

- (void)testSingletonNotNil
{
    STAssertNotNil(c, nil);
}

- (void)testSingletonProperty
{
    STAssertEqualObjects(c, [iCode_Framework alloc], nil);
}

- (void)testIBufCapacity
{
    STAssertEquals(c.iBufCapacity, 1024u, nil);
}

@end

このアプローチの利点は、他のクラスとまったく同じように使用されるため、テスト用にモックできることです。

于 2012-07-14T23:38:27.320 に答える