In my app I have multiple subclasses of UITableViewCell
. I use a factory class to instantiate the right type and provide them to whoever needs them. The factory class gets an argument which is a data object that represents the cell. I consider that to be alright since the factory is part of the controller.
Cellfactory* factory = [[Cellfactory alloc ] initWithData:data];
The problem comes when that data object is mapped with its view representation inside the factory class. I've thought of 2 solutions:
to keep a dictionary with the mapping, the keys would be a string representation of the data object class and the value would be a string representation of the view class responsible for rendering that data object. That way when the factory is asked to provide a view based on a data object, it will know what class to instantiate by looking into the map dictionary. The down side is that if you add another data object, you need to modify the factory method too (add a new entry in its dictionary)
a more flexible solution that would not require to modifying the factory method no matter how many new objects you add, would be to keep the name of the view class responsible for rendering the data object inside the data object itself. But that couples the model to the view and somehow doesn't feel right for the model to have to provide info regarding the rendering.
So, which of these two are a better long term alternative? Is there any other alternative that beats them both?
You could do something similar to NSValueTransformer, and allow classes to be registered. Something like + (void)setViewClass:(Class)viewClass forDataClass:(Class)dataClass;
This is more flexible because you can register these from wherever is easiest for each view and data class. It also uses Class objects rather than strings, which is cleaner and less typo-prone. You can then build a dictionary similar to the one you describe, but with Class objects rather than strings. You can use a Class object just like the class itself. eg. [[viewClass alloc] init]
.
Incidentally, you don't really need a factory object for this; you can just use a factory method, like + (UITableViewCell *)cellForData:(id)data
.