2013-05-11 7 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

+0

문자열을 만드는 대신 인덱스의 NSArray를 사용할 수 있습니다. ObjA에 대한 다양한 속성을 만들기 위해 인덱스를 반복해야하므로 NSArray가 더 쉬울 수도 있습니다. 하지만이 문제는 제외하고이 컬렉션을 만들려면 어떻게해야합니까? –

+0

내가 제안한 접근 방식에 어떤 문제가 있습니까? – trojanfoe

답변

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

에 대한 위키 기사를 읽을 수 있지만, 당신은 객체의 더 나은 조작을 원하는 경우, 다른 패턴을 사용해야합니다 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 
관련 문제