Psychoman Posted January 9, 2011 Share Posted January 9, 2011 (edited) ContextIt's been a while since I have discovered AutoIt and I believe it is absolutely awesome.After developing some little scripts I have come across the need for developing a bigger application.I quickly understood I would have to apply the Model View Controller design pattern.I have quite some experience with Object Oriented programming languages however I lack knowledge of functional scripting languages.As the forums didn't give me the answers I searched for I developed a little application which tries to apply the MVC design pattern.My requestCould following example of the application of the MVC in AutoIt be valdated?Every remark, comment, input would be greatly appreciated:- the application of the MVC pattern in AutoIt- the structure of the code- the coding conventions I used- general usage of AutoIt- ...Functionality of the programThe application stores a configuration of a database in it's state:- database server- database name- database username- database passwordWhen the program is launched, an INI file is read with contains the configuration.The data of the INI file is stored in the state of the program.The user can then consult and adapt the configuration.Once the application is stored the program writes back it's state to the INI file.Graphical User InterfaceThe main windowThe use can consult the stored configuration of modify the configuration.The consultation windowAll the user can do is close the window.The modification windowHere the user can modify the configurationOnce modified, the user can confirm the modification or cancel it.Most important au3 files/Application.au3_________________________The application is launched with this file/model/Configuration.au3_________________Manipulation of the state of the application/model/ConfigurationFile.au3_____________Writing the state to an INI file/view/Main.au3___________________________The main window/view/ConsultConfiguration.au3___________The window to consult the configuration/view/ModifyConfiguration.au3____________The window to modify the configuration/controller/Main.au3_____________________The controller of the main window/controller/ConsultConfiguration.au3_____The controller of the window to consult the configuration/controller/ModifyConfiguration.au3______The controller of the window to modify the configurationComments on the structure of the code/model/Configuration.au3The state of the application is stored in following variablesGlobal $stateDatabaseServer Global $stateDatabaseName Global $stateDatabaseUsername Global $stateDatabasePasswordThe state should only be accessed via following operations_GetDatabaseServer() _SetDatabaseServer($parDatabaseServer) _GetDatabaseName() _SetDatabaseName($parDatabaseName) _GetDatabaseUsername() _SetDatabaseUsername($parDatabaseUsername) _GetDatabasePassword() _SetDatabasePassword($parDatabasePassword)As the model contains all data, it is the only one who can perform validations on it_IsConfigurationValid($parDatabaseServer, $parDatabaseName, $parDatabaseUsername, $parDatabasePassword)Views only contain presentation logic.The initialization of a view can be parametrized as it can contain dynamic data.Func _View_ConsultConfiguration_Create($parDatabaseServer, $parDatabaseName, $parDatabaseUsername, $parDatabasePassword)Although the views catch the events of the GUI, it's the associated controller who handles the functionality behind the event.Catching the events of the main windowFunc _View_WindowWelcome_HandleEvents() While $guiMainLoop Local $varMessage = GUIGetMsg() Select Case $varMessage = $GUI_EVENT_CLOSE _Controller_Main_WindowClosed() Case $varMessage = $guiMainButtonConsult _Controller_Main_ButtonConsultPressed() Case $varMessage = $guiMainButtonModify _Controller_Main_ButtonModifyPressed() EndSelect WEnd EndFuncHandling the events by the controller.The controller can access the public functions of the model layer.Func _Controller_Main_WindowClosed() _View_Main_Destroy() _WriteConfiguration() EndFunc Func _Controller_Main_ButtonConsultPressed() _View_Main_Destroy() _Controller_ConsultConfiguration_Initialize() EndFunc Func _Controller_Main_ButtonModifyPressed() _View_Main_Destroy() _Controller_ModifyConfiguration_Initialize() EndFuncThe sourcecodeThe sources of this program are included as an attachment of this post.I will give my best try to answer all your questions and take into account your remarks.Thanks to all of you who will help me out with this.I really appreciate your help.example.zip Edited January 26, 2011 by Psychoman mvk25 1 Link to comment Share on other sites More sharing options...
hench Posted January 9, 2011 Share Posted January 9, 2011 it seems well done to me !! this is very clear and convenient ! Link to comment Share on other sites More sharing options...
MvGulik Posted January 9, 2011 Share Posted January 9, 2011 1)I see you using the Local keyword instead of the Global keyword at Root level to define some variables.Just making sure you know. These variables will just be as Global as if you would have used the Global keyword on them.(Seem you used Local to indicate there local purpose to the WVC section in which they belong. But ... never forget that they are in reality Global's. To the whole code, as includes don't have there own scope.) "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ... Link to comment Share on other sites More sharing options...
Psychoman Posted January 11, 2011 Author Share Posted January 11, 2011 Thanks for the constructive replies. I have adapted the sourcecode to reflect you remark MvGulik. None of the global variables should be declared as local.example.zip Link to comment Share on other sites More sharing options...
hook Posted April 6, 2019 Share Posted April 6, 2019 (edited) I have no comments regarding the MVC structure, since I am "relearning" it all again - since I programmed many years ago... However, I wish that in the example, that there had been more "Databases" to be saved into the Ini - showing how to manage multiple "records"... This ofc applies to all patterns and solutions as something to be taken care of - and not specifically to MVC.. Two ways to do this I think.. 1) Either the data can be saved into the INI after each Modify - and before modifying, one can choose which database ("record") it is one want to edit or 2) To me atm more complicated, but might be the most beautiful way(?) in regards to Patterns... There need to be implemented an array or the like (added to the state) which is maintained in the Model/configuration until the final writing it to the INI file I think it is a nice and clear example, thanks Edited April 6, 2019 by hook Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now