objective-cdesign-patternsdata-formats

Builder pattern in objective C


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.


Solution

  • 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];
        }
    }