2013-05-11 98 views
1

我在設計我的應用程序時遇到了一些結構困境。我想要使​​用一系列嵌套循環來創建大量的自定義對象。一旦創建了這些對象,我想將它們全部存儲到這些對象的集合中。從父項初始化子對象

可視化:

@interface CollectionOfObjectA : NSObject 
@property (nonatomic, strong) NSArray *reference; 
@end 
@implementation CollectionOfObjectA 
-(CollectionOfObjectA *)init{ 
    NSMutableArray *ref = [[NSMutableArray alloc] init]; 
    for(int i=0; i < largeNumber; i++){ // There will be nested loops. 
     NSString *str = @"string made from each loop index"; 
     ObjA *obj = [[ObjA alloc] initWithIndexes: str]; 
     [ref addObject: obj]; 
    } 
    self.reference = [ref copy]; 
} 
@end 

@interface ObjA : CollectionOfObjA 
// several properties 
@end 
@implementation ObjA 
-(ObjA *)initWithIndexes:(NSString *)indexes{ 
    self = [super init]; 
    // Use passed indexes to create several properties for this object. 
    return self; 
} 
@end 

什麼會是如何創建這個對象,它是子對象的集合的最好方法?我在使ObjA成爲CollectionOfObjectA的一個孩子時是否不正確 - 它是否應該是另一種方式?任何幫助將不勝感激。

+0

爲什麼使用字符串來初始化這些對象?使用容易使用的東西不是更好嗎? – trojanfoe 2013-05-11 09:14:26

+0

我可能會使用索引的NSArray,而不是創建一個字符串。我需要遍歷索引來創建ObjA的各種屬性,所以也許NSArray會更容易。但是,除了這個問題,我該如何去創建這個集合呢? – 2013-05-11 09:22:08

+0

我不是你自己建議的方法有什麼問題嗎? – trojanfoe 2013-05-11 09:48:43

回答

1

好吧,我的建議:我有近30個自定義對象。像事件一樣。之後,我創建了可以創建所有這些類的Factory。而且這個類Factory有方法:getAllObjects

像這樣:

#include "CustomEvent.h" 
@interface EventFactory 

+(NSArray*)allEvents; 

@end 

@implementation EventFactory 

-(CustomEvent*)firstEvent{/*something here*/} 
-(CustomEvent*)secondEvent{/*yes, you should init custom object here*/} 
-(CustomEvent*)thirdEvent{/*and after that you can put them*/} 
/* 
... 
*/ 
+(NSArray*)allEvents{ 
     EventFactory* factory = [[EventFactory alloc]init]; 
     return @[ 
       [factory firstEvent], 
       [factory secondEvent], 
       /*...*/ 
       [factory lastEvent] 
       ]; 

} 
@end 

在這裏,我回到NSArray,因爲我不需要,實際上,知道他們的東西。他們已經有處理程序,他們訂閱了自定義通知。您可以返回NSDictionary以獲得更好的訪問權限。

P.S:爲更好的解釋,你可以閱讀有關Factory pattern

在wiki文章但是,如果你要的對象更好的操控,你應該使用其他模式:Composite pattern

我是什麼意思?

@interface EventCollection{ 
    NSMutableArray* YourArray; 
} 

-(void)addCustomEvent:(CustomEvent*)event atPosition:(NSInteger)position; 
-(void)removeCustomEventAtPosition:(NSInteger)position; 
-(void)seeAllEvents; 
-(void)seeAllPositions; /*if you want*/ 
-(void)doesThisPositionAvailable:(NSInteger)position; 

@end 

@implementation EventCollection 

-(void)addCustomEvent:(CustomEvent*)event atPosition:(NSInteger)position{ 
    /*maybe you should check if this position available*/ 
    if ([self doesThisPositionAvailable:position]){ 
     /*add element and save position*/ 
    } 
} 

-(void)removeCustomEventAtPosition:(NSInteger)position{ 
    if (![self doesThisPositionAvailable:position]){ 
      /*destroy element here*/ 
    } 
} 

-(void)seeAllEvents{ 
    /*yes, this method is the main method, you must store somewhere your objects. 
     you can use everything, what you want, but don't share your realization. 
     maybe, you want use array, so, put it as hidden variable. and init at the initialization of your collection 
    */ 
    for (CustomEvent* event in YourArray){ 
     [event description]; 
    } 
} 

@end