开发者

Combining generic MVP pattern with abstract factory pattern

开发者 https://www.devze.com 2023-01-12 14:15 出处:网络
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?

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<ISom开发者_Go百科eVew>
{}

public SomeOtherPresenter : Presenter<ISomeOtherView>
{}

public class Factory()
{

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

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消