Let a model instance choose appropriate view class using category. Is it good design?
Posted
by
Denis Mikhaylov
on Programmers
See other posts from Programmers
or by Denis Mikhaylov
Published on 2012-09-18T13:36:36Z
Indexed on
2012/09/18
15:53 UTC
Read the original article
Hit count: 228
mvc
|objective-c
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!
© Programmers or respective owner