4

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.

  • Please post your code so we can have a better understanding. Also which languages are we talking about? – Filipe Giusti Nov 4 '14 at 21:38
  • I am working in android. But the design technique used should not depend on language. – user4057066 Nov 5 '14 at 7:20
  • 5
    Actually it's totally dependent on language, each language has a best way to share common code. You could use direct inheritance, module includes, delegation, high order functions, etc. – Filipe Giusti Nov 5 '14 at 15:36
  • Agreeing with Filipe Giusti - it totally depends on the flexibility of the language (lambda, etc), and framework (UI bindings, and ability to programmatically change it at runtime). Note that some frameworks try to make presentations (views) as effortless as possible, which means these frameworks encourage people to use different presentations for things that are justifiably different, even if there are some overlaps in between. – rwong May 4 '15 at 22:56
3

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.

0

On Ruby I would either include a third class that shares the common code between both presenters or delegate, depending on how much shared code and what dependencies the presenters have. Probably by you description I would make the ListPresenter delegate to the DetailsPresenter whether methods they have in common.

0

The proper approach for this kind of situation is to have a base presenter and base view, where you can do the common stuff.

class BasePresenter<T:BaseView>
{
    BasePresenter(T view)
    {
        View = view;
        View.Presenter = this;
    }

    T View { get; private set; }    
}

class BaseView<T : BasePresenter> : [base view component such as asp net component]
{
    T Presenter { get; set; }  

    protected void ShowProgress()
    {
       ...
    }

    protected void HideProgress()
    {
       ...
    }
}

class ListPresenter : BasePresenter<ListView>
{
    ListPresenter(ListView view) : base(view)
    {

    } 

    void FetchData()
    {
        .....
        View.ShowList(data);
    }
}

class ListView : BaseView<ListView>
{
    void ShowList(List<object> data)
    {
        .....
    }
}

Without having a look at your code base, I can't say more on how to organize the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.