Reputation: 2495
I am using a couple of different services to display articles from feeds. They can all be displayed using one view controller, however I must make a property indicating from which service the user is coming, and to check it every time I want to do something with those articles.
I have experience in this method - and believe me there are a lot of spaghetti code, just to check out which service it is, to decide what do to for each service, it's a little messy.
So I have 2 options:
1st - re-use the ArticlesViewController for all services, but check from which one is the articles coming from, and do different things based on that when I want to edit those articles.
2nd - make FacebookArticlesViewController,TwitterArticlesViewController, SinaWeiboArticlesViewController (just examples), for each service, and separate the logic.
What do you think is the best approach?
Upvotes: 1
Views: 105
Reputation: 10475
1 st option is better because you do not duplicate any code. In future if you have to change the UI you'll have to change it in only one place. If you go with second approach, you'll have to change stuff in all view controllers and theres a big chance you'll miss something out.
Also, what you can do is, create a parent article view controller with all common stuff and inherit separate article view controllers only for those types of articles which need lot of customization.
And if the differences in your articles are largely related to business logic and not UI, it would be better to have separate business layer managing all this stuff. And of course, you can implement same kind of inheritance i mentioned in above paragraph for these business layers.
Upvotes: 1
Reputation: 385610
Surprise third option!
Make a protocol or a base class named ArticlesViewControllerSourceDelegate
. For each place in ArticlesViewController
where you'd have logic that varies based on the article source, add a selector to ArticlesViewControllerSourceDelegate
.
Then make an implementation or subclass of ArticlesViewControllerSourceDelegate
for each article source.
Upvotes: 4