Why create an interface in Presenter if we can just return the value and set it in the view (MVP Structure)

Two months passed, I worked with MVP Structure to create Android applications.

As explained in every link I found regarding MVP, the Presenter class takes care of all business logins and data (from the Model class), I realized this and started working.

One of the advantages shown in the tutorials, I want to emphasize here that MVP simplifies unit testing, because there is no dependency on the views (Fine, I also understood this).

What I did not understand is why I need to make an interface for updating views from the host, whereas I can just call the host method, which will return a value, and I can set it there?

Let's take advantage of what I said above (Unit testing). Using these interface modules will be more problematic, since the method will require the implementation of an interface to complete an operation that we do not have in unit testing (I know that the Instrumental test is also part of the unit test, but they are only told about non-ui testing).

I just prefer to call the presenter method and get the value and set it in the fragment itself or in the action itself, where when creating the interface creates another level of complexity and an unnecessary declaration of the interface, all the presentation operations should be implemented. This is a kind of disappointment.

, , -, . , . . . - .

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*/);
    }
}

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;
    }
}

,

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
    }

}

, .

. .

+4
1

, MVP, , .

, SOLID , , , .

, .

, :

, . , , . , , . , M, V P, .

, . ProductListCustomView, ProductListFragment ProductListActivity. , . , .

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);

}

}

, , Presenter, . , (, LongProductListPresenter, ShortProductListPresenter ..)

MVP , , Android.

.

, , - , .

MVP google . , . , .

- ( ).

, , , .

.

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

+3

Source: https://habr.com/ru/post/1649494/


All Articles