views:

256

answers:

3

Hi all,

I'm experimenting with implementing a lightweight mvp framework with Delphi 2009.

Views are passive but supports databinding (via an interface property).

I'm facing a dilemna: I've several very similar views/presenter/model triad, ie :

order form and a customer form = behavior and logic is the same but the datasource for databinding is different and the form title too. the datasource is a common property for all my models so it's not a problem, to set the form title, I'm forced to hard code it in my presenter InitView method

All is working good, but I'm in a situation where I have several simple mvp triads very similar. I want to refactor it but in that case I will have to pass some parameters to the mvp constructor.

So far I'm doing like that :

  1. Create the view
  2. Create the model
  3. Create the presenter and inject model and view in the constructor

In fact, I'm facing a choice :

  1. Having some very generic views/presenter, use them like that but inject 1 or 2 parameters in the constructor
  2. Having some views/presenters superclass, and derive all my similar view/presenter from them and set some specific values in the overriden methods.

Can you give me some hints / advices ?

(sorry if i'm not very clear)

+1  A: 

Fred,

I will choose 1 & 2 in a way that is having an abstract views/presenters that contain generic behaviors and creates abstract functions that could be possible specific behaviors implemented by subclasses.

for example,

  public abstract class AbstractPresenter{
      // subclass will be implemented 
      public abstract void InitView(Model model, View view);
  }

and then you might have sublcasses, OrderFormPresenter and CustomerFormPresneter extends from AbstractPresenter.

public OrderFormPresenter extends AbstractPresenter{
    public void InitView(Model model, View, view){
      // do something specific values 
    }
}

public CustomerFormPresenter extends AbstractPresenter{
    public void InitView(Model model, View, view){
      // do something specific values 
    }
}

Please, correct me if it goes wrong direction. I hope it helps.

Tiger

Tiger
Tiger, thanks for your answer. your solution seems to be my n°2. I agree that it's the clean way to do it, but creating several subclasses just to set form title or a label on a form is a bit overkill from what i see now. Thanks for your help.
Fred
+1  A: 

I'd create a generic view/presenter with parameters and subclass only when needed.

Erick Sasse
+1  A: 

Another approach (and the way that I once solved this problem so it worked very well) is to build a generic "metadata" interface into the model, and the view (either interfaces, or via class inheritance) then use these generic interfaces in your presenter. I chose to use inheritance for my model, and interfaces for my view (was easer to slap a interface on an existing form than to require form/frame inheritance across the board). In my solution, the constructor for the presenter took 3 parameters, the model, the view and the "MVP name". I used the name of the MVP to load settings which were specific to the current scenario.

skamradt