Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6232707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:09:48+00:00 2026-05-24T10:09:48+00:00

I am trying to understand the MVC pattern and this is what I came

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:09:49+00:00Added an answer on May 24, 2026 at 10:09 am

    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):

    MVC diagram

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I am trying to learn the java MVC pattern, but I can't understand
I am having some trouble understanding the MVC Pattern. I do understand we are
I am trying to understand and practice the MVC-pattern to improve my development. So
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I'm trying to understand the MVC pattern and I get the general idea that
Trying to use PureMVC framework and understand MVC concept. Don't understand this sentence: The
I'm learning MVC and trying to follow some tutorials and this video http://channel9.msdn.com/Events/DevDays/DevDays-2011-Netherlands/Devdays002 In
I'm just learning C# and MVC, and trying to understand some examples. @Html.EditorFor(m =>
I'm trying to understand MVC, and learning CI framework. I've some questions about MVC
Trying to understand Ruby a bit better, I ran into this code surfing the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.