"Chau Chee Yang" <
XXXX@XXXXX.COM>a écrit dans le message de news:
XXXX@XXXXX.COM...
| I have created some MVP and it goes okay. However, I still feel it isn't
| that perfect at all. The concepts of Interactors is hard to code in
| Delphi/VCL and some involve changing the WndProc as proposed by
| Joanna.
The Interactors are there to handle any event happening in a visual control
and can either handle straightforward events or it can intercept the WndProc
only if that is deemed necessary to provide correct hendling for things like
stopping focus from leaving an edit if validation of the edit's content
fails.
If I use .NET WinForms controls, then intercepting the WndProc is no longer
necessary as the event model of all controls is much more complete and
includes a Validating event which allows us to return a "cancel" flag to the
control, thus stopping focus leaving the control.
The purpose of the Interactors is to provide a place for event code from
controls without having to place that code on the form. Coding Interactors
should be as simple as coding event handlers on a form.
| The concepts of Selection doesn't fit well into simple model that don't
| have list of items.
In the case of a simple model, then the Selection is deemed to be the value
that is held in the model.
TModel = class
public
property Selection: TSelection ... ;
...
end;
TObjectModel = class
private
fValue: TObject;
public
constructor Create(Value: TObject);
end;
constructor TObjectModel.Create(Value: TObject);
begin
fValue := Value;
Selection.Add(fValue)
end;
TListModel = class
private
fValue: TObjectList;
public
constructor Create(Value: TObjectList);
end;
constructor TListModel.Create(Value: TObjectList);
begin
fValue := Value;
// don't add anything to the selection
// as nothing has been selected in the View yet
end;
| The command/command Set also seems strongly relate
| to the UI element like Button/MenuItem.
Certainly, the Command concept is usually connected with such UI elements,
but don't forget that menu items can be on a context sensitive popup menu on
a single edit control.
You may not have any Commands on a single object/property but you can have a
popup menu on a list box that allows you to Add/Insert/Delete items and it
is for situations like this that the Command/set comes into its own.
| And I found Presenter seems do
| nothing here and it is just an object to hold a list of interactors. The
| role of presenter in MVP seems too simple from my understanding.
The Presenter is there to hold the Model and View and to connect them up, as
well as holding any relevant Interactors.
When "presenting" an object, the calling syntax should be something like
this :
procedure T... AddCustomer;
var
cust: TCustomer;
presenter: TPresenter;
begin
cust := TCustomer.Create;
presenter := TFormPresenter.Create(cust, TCustomerForm);
end;
Calling the presenter's constructor not only creates the presenter, it goes
on to create a Model for the TCustomer value, and an instance of the
TCustomerForm form class that was passed in to the constructor. It would
then go on to hook up any buttons on the form to the Command Set in the
Model and finally, show the form.
Apart from linking up the Customer to the form that will display it, the
Presenter is also responsible for creating and holding a list of Control
Presenters, one for each property of the Customer object, so that each
property is linked.
Of course, because the Control Presenters need an object to act as the value
for the Model, you would need to use a Value Type Framework (Google for
previous discussions in this group on this topic) behind the public
properties of your objects.
You should end up with a form with absolutely no code on it, and a Presenter
that knows how to create a form and link up all the relevant components and
show the form.
Saving the object that was edited in the form is the job of the Save Command
in the Model that is called by the Interractor attached to the Save button
on the form.
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer