MVP Communication between presenters?

前端 未结 3 1732
既然无缘
既然无缘 2020-12-25 13:40

I have a question about how to handle communication between presenters when using MVP. Say I have two MVP-triads. One is a list of products (Triad A) and the other is some g

3条回答
  •  借酒劲吻你
    2020-12-25 14:09

    I'd like to add my point of view, coming with a mobile development background.

    It seems to me that you are working with a typical Master-Slave scenario. What I would suggest is that despite the fact those containers A and B have their own presenters, because obviously they may have their own implementation scopes that needs to be handled in a clean way, they shouldn't directly talk to each other. They may live without each other. However if there is a way to present them at the same time - it would be done through a bigger container that handles them. And this is a key to your problem.

    Declare interfaces that inform about the container A/B changes. Implement them within bigger container (let's call it host) and create a listener that will represent those interfaces to bind the communication.

    Whenever there is a change in container A it informs host through interface, host passes the event to it's own presenter which can run a bigger scope operation like storing the state or sending analytic event to backend and finally it tells host to update the slave where the container B is given the data for update.

    The solution with events is probably very tempting to use, I've done it myself many times, but it comes with a price. If you misuse it you'll just add to technical debt. I would suggest using this mechanism for situations where the "event" is not necessarily bound to any particular handling scenario, but may become a point of interest of other modules that live within your project.

提交回复
热议问题