1

I came across a design pattern "Builder" which I think would fit well to what I would like to do. In my iOS application, I have different Core Data Entities and I would like to able to transform my objects into different data formats. Currently, my plan is to support XML, HTML & JSON. So, I have been thinking to design this solution using a standard design pattern like "Builder". Some of my questions are

  1. Is the Builder pattern the correct one for this problem?
  2. Can I use this pattern in Objective C? Since there is no abstract class notion in Obj C, not sure how to approach this. I am thinking I should use a base class and a protocol to define my abstract methods.

Please, feel free to suggest of any new design solutions that would best suit my needs.

Thanks so much for your suggestions and comments.

Regards, Javid

Edited: One other key point is the final format will comprise of different core data entities. For example, if there is an entity team and player, I need to create a data format that uses both team & player.

user320587
  • 1,277
  • 4
  • 27
  • 52

2 Answers2

1

You can use Categories to extend the behave of Objects.

for example, if you wanted to add the ability stringJSON to a NSDictionary you could do the following:

NSDictionary+JSON.h:

@interface NSDictionary (json)
-(NSString *) stringJSON;
@end

NSDictionary+JSON.m

@implementation NSDictionary (json)
-(NSString *) stringJSON {
    return @"{key:\"...\"}";
}
@end

Now in any .m file you want to use this new method on your NSDictionary Objects you include the category

#include "NSDictionary+JSON.h"

and use like so:

NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"value",@"key", nil];
NSLog(@"%@",[dict stringJSON]);

A few References: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html#//apple_ref/doc/uid/TP30001163-CH20-SW1

http://cocoadevcentral.com/d/learn_objectivec/

jsmp
  • 162
  • 1
  • Hi jsmp, Thanks for suggestion. I think I missed a key point in my problem description. My end goal is create a complex object that will use multiple core data entities. For example, if there is an entity team & player, I would like to create a representation of the team with all players. – user320587 Apr 20 '11 at 22:29
  • This might help http://stackoverflow.com/questions/2362323/json-and-core-data-on-the-iphone – jsmp Apr 20 '11 at 22:43
1

1) Yes, the Builder pattern sounds like a good solution to this problem. The idea behind Builder is that the overall build process is consistent across all data formats but the build details are different.

2) Yes, the Builder pattern is can be used in Objective C. The Director and the various ConcreteBuilder classes would all be normal classes, and the Builder abstract class would be a protocol that the Director uses, if it is represented at all.

You might end up with something like this in the director:

@implementation Director
- (void)construct {
    for (Team* team in self.allTeams) {
        [self.builder buildTeamStart];
        for (Player* player in team.allPlayers)
            [self.builder buildPlayer];
        [self.builder buildTeamEnd];
    }
}
Daniel T.
  • 24,573
  • 4
  • 44
  • 59
  • Thanks Daniel. One quick clarification, the ConcreteBuilder class would implement the Builder protocol correct? – user320587 Apr 21 '11 at 14:57
  • Yes, all the ConcrerteBuilders would implement the Builder protocol. The best way to create the code is to build one ConcreteBuilder at the same time you are building the Director, keeping an eye toward what is common with all building schemes. (The common stuff goes in the Director.) Once you get that working, then making the other two ConcreteBuilders should be easy. – Daniel T. Apr 21 '11 at 20:22