Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to make an interface in Presenter if we can just return a value and set in a View (MVP Structure)

Tags:

It has been two months, i have been working with MVP Structure for creating Android apps.

As explained in every link i found regarding MVP that Presenter class takes care of all business login and data(from Model class), I understood that and started working.

One of the advantages shown in tutorials, i want to emphasise it here that MVP make unit testing easier because there is no dependency of views(Fine i understood that also).

What i did not understand is why to make interface to update views from presenter whereas i can just call a method of presenter that will return a value and i can set it there?

Lets come to the advantage i talked about above(Unit testing). Using those interface unit testing will be more problematic as method will require an interface implementation to complete an operation that we do not have in Unit testing(I know Instrumental test also comes in unit test but i m talking about only non-ui testing).

I just prefer to call a presenter method and get value and set in view in fragment or activity itself where as creation of interface creates another level of complexity and unnecessary interface declaration there all view operations are to be implemented. Its kind of frustrating.

One of the friend i am working with pointed out this problem in my code and told me to look all online references to clarify my mistake. But i want to know how these interfaces is help full in programming practice. Because i can not just digest it. Its becoming a pain in my ass. I have looked all the online references no solution.

Example

Presenter with interface

class Presenter
{

    private ViewInterface viewInterface;

    public void setViewInterface(ViewInterface viewInterface)
    {
        this.viewInterface = viewInterface;
    }

    // Here value is being passed to interface method that is implemented in fragment.
    // No problem with this implementation but why to do it. 
    // It will make unit test problematic as this method needs ViewInterface.
    public void calculate()
    {
        // Some calculation
        viewInterface.updateView(/*pass some paramerer*/);
    }
}

Presenter without interface

class Presenter
{


    // Here just take the value and set the view in fragment
    // Unit test easier just check the returned value.
    public int calculate()
    {
        int result = -1;
        // Some calculation
        return result;
    }
}

Fragment where presenter is being used

class MyFragment extends Fragment{

    ....

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.layout1, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        // just call method and get value to set in a view.
        int result = new Presenter().calculate();
        // set result in a view
    }

}

Please check comments in above code.

Any help will be appreciated. Thanks in advance.

like image 315
chandil03 Avatar asked Jul 29 '16 06:07

chandil03


1 Answers

It has only been a few months, since I made the transition from my own novice code to MVP and I have to say that learning this architecture has made me a better programmer and given me a new appreciation for Interfaces.

I do not have the education or experience, to talk about SOLID, or any other principles in Computer Science, but I can offer you my opinion based on what I have learned and experienced.

Having interfaces for each component makes it very easy to see what your application does.

Looking at the following contract, found here :

I know exactly WHAT is happening in the entire module of this application. If I care about HOW things are done, I can look at the implementation. When writing unit tests, I don't care HOW things work, just that they DO work. I can change each component, the M, V, or P, without breaking anything in my application.

There may be a use case when we have multiple views for the same Model. We could have a ProductListCustomView, a ProductListFragment and a ProductListActivity. We may choose one of these depending on the device that the application is running on. All of them implement the same interface, so it is easy to make the change, even at runtime.

public class ProductListContract {

public interface View {
    void showProducts(List<Product> products);

    void showAddProductForm();

    void showEditProductForm(Product product);

    void showDeleteProductPrompt(Product product);

    void showGoogleSearch(Product product);

    void showEmptyText();

    void hideEmptyText();

    void showMessage(String message);

}

public interface Actions {
    void loadProducts();

    void onAddProductButtonClicked();

    void onAddToCartButtonClicked(Product product);

    Product getProduct(long id);

    void addProduct(Product product);

    void onDeleteProductButtonClicked(Product product);

    void deleteProduct(Product product);

    void onEditProductButtonClicked(Product product);

    void updateProduct(Product product);

}

public interface Repository {
    List<Product> getAllProducts();

    Product getProductById(long id);

    void deleteProduct(Product product);

    void addProduct(Product product);

    void updateProduct(Product product);

}

}

Looking at the above example, I do NOT think that an interface for the Presenter is necessary, unless you have more than one implementation of the presenter. I can not think of a scenario where a View could be associated with multiple Presenter Types, ( eg LongProductListPresenter, ShortProductListPresenter, etc )

A few months after completing an application using MVP, you may decide to change some of your views, by converting them to custom views, or converting your Layouts to the latest big thing released by Android.

You may also decide to use a different library for storing your data, or networking.

You may want to make changes to the logic within your presenter, but it's unlikely that you will ever wake up one morning and decide that you need a brand new Presenter for your application.

The MVP todo sample by google uses an interface for the presenter. Therefore, the vast majority of developers, will do the same. If they one day change this sample app, then everyone else will quickly follow suit.

At the end of the day, you are the architect and how you build your application is your (or your boss's) decision.

I hope you have gained more confidence in making your own decision, regardless of which choice you make.

Here is another article on the subject.

http://blog.karumi.com/interfaces-for-presenters-in-mvp-are-a-waste-of-time/

like image 81
DivineChaos Avatar answered Sep 28 '22 03:09

DivineChaos