开发者

Delphi 7 - trying to understand the MVC pattern [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I am trying to understand the MVC pattern and this is what I came up with. If you could be so kind and tell me if I did it correctly, suggest some improvments, or diss me for failing completely, I'd be more than happy!

Here's a link to the project (Delphi开发者_JAVA技巧 7): http://www.sendspace.com/file/ynpgre


I have rewritten your project to use MVC.

Main project file:

var
  Model: TModel;
  Controller: TController;
begin
  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);

  Model      := TModel.Create;
  Controller := TController.Create(Model, MainForm);

  Application.Run;

  Controller.Free;
  Model.Free;
end.

Model:

TModel = class(TSubject)
private
  FData: TStrings;
public
  constructor Create;
  destructor Destroy(); override;
  procedure AddLine(AText: string);
  property Data: TStrings read FData; // Do not write to this directly, since it doesn't call notify!
end;

Controller:

TController = class(TObserver)
private
  FModel: TModel;
  FView:  TMainForm;
public
  constructor Create(const AModel: TModel; AView: TMainForm);
  destructor Destroy(); override;
  procedure ButtonClick(Sender: TObject);
  procedure Refresh(ASubject: TSubject); override;
end;

The main form is working as the View, i have removed all code from it.

The controller registers itself as an observer and does all the logic:

constructor TController.Create(const AModel: TModel; AView: TMainForm);
begin
  inherited Create();
  FModel := AModel;
  FView  := AView;
  FModel.Register(Self);
  FView.Button1.OnClick := ButtonClick;
  FView.Button2.OnClick := ButtonClick;
  FView.Button3.OnClick := ButtonClick;
end;

destructor TController.Destroy;
begin
  FModel.UnRegister(Self);
  FView.Button1.OnClick := nil;
  FView.Button2.OnClick := nil;
  FView.Button3.OnClick := nil;
  inherited;
end;

procedure TController.Refresh(ASubject: TSubject);
begin
  FView.ListBox1.Items.BeginUpdate;
  try
    FView.ListBox1.Items.Assign(FModel.Data);
  finally
    FView.ListBox1.Items.EndUpdate;
  end;
end;

procedure TController.ButtonClick(Sender: TObject);
begin
  if Sender = FView.Button1 then begin
    FModel.AddLine('Hello');
  end else
  if Sender = FView.Button2 then begin
    FModel.AddLine('Hello World!');
  end else
  if Sender = FView.Button3 then begin
    FModel.AddLine(DateToStr(Now));
  end
end;

I cheated a little bit with the View <-> Controller relationship, but you should get the general idea :).

  • The Controller has references to both View and Model.
  • The View knows nothing about the Model.
  • The Model knows nothing aboug the View.

PS: A diagram for the MVC pattern (the dotted lines represent Observer/Subject relationships):

Delphi 7 - trying to understand the MVC pattern [closed]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜