How to reuse common code between presenters

mvp

In my application there are 2 views both have 70% common fields like name, icon etc. One view is about list( contains 2-3 fields). On clicking the list , the next view which is the details should be called.

Other functionality in both views is same. e.g while loading , presenter will be called to load data from the url. (The URL for both views is different).
The common functionality includes
1. Load XML data from URL
2. Set the loading progress bar in activity
3. Removing the progress bar.
3. 50% methods for setting the activity views will be common.
4. Call the xml parser

The differences will be view specific:
1. The fields are only 50% common.
2. the action on button click

My doubt is shall I use 2 presenters or one presenter for both the views. Many methods are different , while many are same in the 2 presenters.

Which is the correct design strategy?:
1. Shall I use helper class
2. Shall both presenters have the same base class.
3. Shall i use the startegy pattern?
What is the right way to do it in MVP? Or all are correct.

Best Answer

I would suggest starting off by writing your two presenters independently, then refactoring to remove duplication. This could be done either by moving code into one or more helper classes, or by introducing a new abstract ancester class for your presenters. Which of these is most appropriate will depend on details of your project that aren't in your question, but unless there are good reasons to favor inheritance I'd usually go for the former.

Related Topic