Delphi 7 - trying to understand the MVC pattern
-
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 7): http://www.sendspace.com/file/ynpgre
-
Answer:
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):
Pateman at Stack Overflow Visit the source
Related Q & A:
- How do I convert a PDF file to PDF/A in Delphi?Best solution by softwarerecs.stackexchange.com
- How to scan image in Delphi?Best solution by Stack Overflow
- Will 7.5 trucks fit on a 7.75 deck?Best solution by answers.yahoo.com
- How to easily install the latest Delphi?Best solution by Yahoo! Answers
- How to install tomcat 7 in windows 7?Best solution by ntu.edu.sg
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.