Let a model instance choose appropriate view class using category. Is it good design?
- by Denis Mikhaylov
Assume I have abstract base model class called MoneySource. And two realizations BankCard and CellularAccount. In MoneysSourceListViewController I want to display a list of them, but with ListItemView different for each MoneySource subclass.
What if I define a category on MoneySource
@interface MoneySource (ListItemView)
- (Class)listItemViewClass;
@end
And then override it for each concrete sublcass of MoneySource, returning suitable view class.
@implementation CellularAccount (ListItemView)
- (Class)listItemViewClass
{
return [BankCardListView class];
}
@end
@implementation BankCard (ListItemView)
- (Class)listItemViewClass
{
return [CellularAccountListView class];
}
@end
so I can ask model object about its view, not violating MVC principles, and avoiding class introspection or if constructions.
Thank you!