Search code examples
genericsmvpabstract-factory

Combining generic MVP pattern with abstract factory pattern


Is there some way to solve my code below? I'm kinda stuck.
How can I use a factory to create generic presenters, is it even possible without a non generic base class?

public abstract class Presenter<T>
{}

public SomePresenter : Presenter<ISomeVew>
{}

public SomeOtherPresenter : Presenter<ISomeOtherView>
{}

public class Factory()
{

  public ??? CreatePresneter(int runTimeValue)
  {
    if (runTimeValue == 1)
      return new SomePresenter()
    else
      return new SomeOtherPresenter()  
  }

}


Solution

  • Many times generic base classes need non-generic bases, so write

    public abstract class Presenter
    {}
    
    public abstract class PresenterOf<T> : Presenter
    {}
    

    and use PresenterOf for your presenters.

    Then CreatePresenter can return Presenter. Obviously, this only helps if ISomeView and ISomeOtherView have a base class that Presenter can work on.

    If these views and presenters don't have commonality in the interface, then using one factory to make them all might not make sense.