简体   繁体   English

如何使用MVP模式处理不同的接口实现?

[英]How to handle different interface implementations using MVP pattern?

I have a design that I am working on and wanted to be clear on how one should handle multiple implementations of an interface using MVP pattern. 我正在进行一项设计,希望弄清楚如何使用MVP模式处理接口的多种实现。 Here is my situation: 这是我的情况:

There is a base "Tank" interface which defines basic functionality of what a tank should do. 有一个基本的“ Tank”界面,该界面定义了容器应执行的基本功能。

public interface ITankView
{
  public string TankName
  public double TankLevel
  public double TankSize
  ... ext ...
}

My presenter accepts implementations of this interface (ie - the "Tank" view): 我的演示者接受此接口的实现(即-“坦克”视图):

public class TankPresenter
{
  ITankView tank;
  public void TankPresenter(ITankView tank)
  {
    this.tank = tank;
  }

  public void DoStuffWithaBasicTank()
  {
     tank.TankName = "This is a basic Tank!"
     tank.TankSize = 100;
     tank.TankLevel = 50;
  }
}

I also have multiple implementations of the ITankView interface: 我也有ITankView接口的多种实现:

// Just a basic tank with a level:
public class BasicTankView, ITankView
{
   public string TankName {get; set;}
   public double TankLevel {get; set;}
   public double TankSize {get; set;}
}

// The "Advanced" version of a tank:
public class MixableTankView, ITankView
{
   public double TankName {get; set;}
   public double TankLevel {get; set;}
   public double TankSize {get; set;}
   public double MixingSpeed {get; set;}
}

So my question is how do I handle the "Advanced" MixingTankView in the TankPresenter while adhering to the MVP patter and best practice? 所以我的问题是,在遵循MVP模式和最佳实践的同时,如何在TankPresenter中处理“高级” MixingTankView?

For example, if I instantiate the TankPresenter with a MixingTankView how would you access the special "Advanced" functionality? 例如,如果我用MixingTankView实例化TankPresenter,您将如何访问特殊的“高级”功能?

public MixingTankView view = new MixingTankView();
public TankPresenter Presenter = new TankPresenter(view );


public class TankPresenter
{
  ITankView tank;
  public void TankPresenter(ITankView tank)
  {
    this.tank = tank;
  }

  public void DoStuffWithaMixingTank()
  {
     tank.TankName = "This is a mixing Tank!"
     tank.TankSize = 100;
     tank.TankLevel = 50;
     tank.MixingSpeed = 75; // This does not work as it's not declared in ITankView!!
  } 
}

I'm thinking I would need to add in a presenter for each and every type of Tank. 我想我需要为每种类型的Tank添加演示者。 So in this example that would be a IBasicTankPresenter and a IMixingTankPresenter but I'm still a little confused on how that would work. 因此,在此示例中,它将是IBasicTankPresenter和IMixingTankPresenter,但对于如何工作我还是有些困惑。 Also 90% of the functionality between the two tanks is identical so it seems redundant. 两个水箱之间的功能也有90%是相同的,因此显得多余。 In reality I have more than just 2 tanks and the actual implementation is much more complex but each tank type only has slight differences. 实际上,我拥有的坦克不止2个,实际的实现要复杂得多,但是每种坦克的类型只有微小的差异。

My Goals are: 我的目标是:

  1. To reduce code complexity and coupling/inter-dependance of code 减少代码复杂度和代码的耦合/相互依赖
  2. Adhere to the MVP patter to some degree 一定程度上遵守MVP模式
  3. Not repeat myself a bunch of times with each tank type. 每种坦克类型都不要重复很多次。
    • I'd like to reduce the amount of code for my own sanity 我想减少代码量以保持自己的理智
    • Do I really need 1 presenter for every 1 tank view? 每1个战车视图我真的需要1个主持人吗?

First, you will need to add an interface for the MixableTankView - 首先,您需要为MixableTankView添加一个接口-

public interface IMixableTankView : ITankView
{
    double MixingSpeed {get;set;}
}

Then, you can simply use generics. 然后,您可以简单地使用泛型。 You start with your basic presenter, only now you change the ITankView to a type parameter: 您从基本的演示者开始,直到现在将ITankView更改为类型参数:

public class TankPresenter<TTankView> where TTankView : ITankView
{
    TTankView tank;
    // basic implementation here
}

and add another presenter that inherits it: 并添加另一个继承它的演示者:

public class MixingTankPresenter : TankPresenter<IMixableTankView>
{
    // IMixableTankView special implementation here
}

You do this for every different implementation of the ITankView - This way, most of the code will still be only in the base TankPresenter , and you get to keep MVP principles throughout your project. 您对ITankView每个不同实现都执行此ITankView -这样,大多数代码仍将仅位于基本TankPresenter ,并且您将在整个项目中保持MVP原则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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