Recent

Author Topic: How to separate logic from GUI  (Read 858 times)

oldcastle

  • New member
  • *
  • Posts: 8
How to separate logic from GUI
« on: April 16, 2022, 11:39:49 pm »
Hi,

I was reading this thread https://forum.lazarus.freepascal.org/index.php/topic,39981.0.html, and I saw an interesting opinion here.

I recommend you to separate logic from GUI.  I mean: create an UNIT with classes, records and procedures to create, manage, reproduce, load and save MIDI files. Then USE that unit in the FORM unit.  That will make it more easy to understand and to improve.

Are there any documentation / tutorial / example on how to separate logic from GUI? I think that is very important, especially for beginners like me.

Cheers

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: How to separate logic from GUI
« Reply #1 on: April 17, 2022, 04:57:56 am »
It's easy to say it. Separate logic from gui. But is not that easy sometimes the code is for the gui and user interaction and that in my opinion is fine to have it in the form.

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: How to separate logic from GUI
« Reply #2 on: April 17, 2022, 05:28:11 pm »
You might want to take a look at zur MVP pattern: https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter

Basically you Hey to model your system in an abstract Model. Like if you have a chess game, you have a unit "game" which has types and functions to store, manage and modify the state of the board pieces and other game state (whos turn it is, if a castling can be performed etc)
Then your form class is in the mvp model the presenter, it just reacts to the user inputs and "tells" the model to perform the moves. Note that all the logic if the move is valid or not is in the model, so if the user tries an invalid move the model tells the presenter that it cannot be performed and no state has been changed

Lastly the view is basically the lcl form managed by the widgetset so the connection between presenter and view is simply modifying the parameters of the lcl classes

 

TinyPortal © 2005-2018