从父项初始化子对象

问题描述:

我在设计我的应用程序时遇到了一些结构困境。我想要使​​用一系列嵌套循环来创建大量的自定义对象。一旦创建了这些对象,我想将它们全部存储到这些对象的集合中。从父项初始化子对象

可视化:

@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

好吧,我的建议:我有近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