简体   繁体   中英

In MVP design, how can two presenters talk to each other without using a singleton pattern or dependency injection?

I am trying to become a more 'well rounded' programmer by learning new ways to programming or by using two patterns together that I have never done before.

I have used the MVP or MVVP pattern many times on many projects. Every time I have used MVP, it has been coupled with either a singleton design or a DI design using a container of some design. In Both cases I don't have to worry about getting another presenters specific instance as I can easily get it through my CTOR using DI, or by getting it off the static type by doing something along the lines of 'SomePresenter.Instance'.

Assuming I want to stick with a MVP design, how can I access another presenter without using a singleton design or DI? The only thing I can think of is using some sort of event system where a presenter has a bunch of custom event and those event are loaded into an event aggregator. So if I want to set a property on a presenter, using the event aggregator, I raise that specific event with its specific arguments that then gets triggered into the other presenter. This way each presenter still stays separated so that the system is loosely coupled and yet things can still properly communicate.

This is just something off the top of my head though and is most likely NOT ideal or the best. What I am looking for is a design in which I can solve my root question "Assuming I want to stick with a MVP design, how can I access another presenter without using a singleton design or DI?".

I am programming in C#, so its possible C# has some tools that makes this possible but only in C#. I am trying to learn more by doing things I am not used to, and hopefully after seeing some design patterns and or design solutions below, I can become a better programming by having more solutions that I can use in the future.

There are many ways to do this. You don't mention whether you're using MVP as Windows forms, or Web Forms, since it makes a difference in many ways.

In Windows forms, a common method is to use event aggregators, which you've mentioned.

I'm not sure why you wouldn't want to use Dependency Injection, as it's a very solid (no pun intended) design pattern. I would prefer to use this in nearly all cases. If you don't want to use a heavy container, you can write a simple one of your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM