About design patterns ...
-
wrote on 13 May 2019, 21:42 last edited by
Hi.
I´m pretty new to C++ (about 1 year), and all my console test programs did not needed to use a kind of design pattern...
Now, I´m giving a try creating a window program that share data with sqlite, actually I have separated in another class all the function that allows me to operate with sql and files... Well until here everything is "readable and mind clear", but my program is growing and I´m afraid I will get a mess earlier than later (or maybe not)..My question is, do even I need to apply a pattern design?... if so... what is the easiest pattern to start with?. my program is a student score register and they can take their exams, those scores will be stored into a db, then teachers can access with their accounts and check for the student's scores.
Actually I´m fine with SQL, I can do queries and works... I just don't want to get a mess of my program..
Thanks!
-
wrote on 13 May 2019, 22:22 last edited by
For organization design patterns I usually think in terms of separation of concerns. For small apps its not that big of a deal, but as an app grows it can become more and more unwieldy.
I like the MVC design pattern (model, view, controller). I have seen many different variations on this and even different term definitions. Anyway, model is typically data or database. View is gui or user interface or even client interface. Controller is business logic. The controller controls the data flow from view to/from model. It also is the meat of where application specific logic occurs. The view and model don't even know about each other. A lot of the time the model and view don't know about the controller either. This encapsulation can help in a lot of ways. I have found applications can more easily be automated when the controller is where all the magic happens. I have even built apps where the controller doesn't know about the view specifically. This was done using a pub/sub system (which I don't think Qt has). I don't think you need to go that far, but it can be useful to separate things in order to change parts later. For instance, you might design your code to be able to use either QWidgets or QtQuick interchangeably or even act as a server without changing control logic.
There are other strategies for separating concerns and a quick search on Google will probably turn up dozens of different ideas. So, TL;DR, I like MVC. Have not used it much with Qt though. I am mostly maintaining existing projects written by other people. I have used it extensively in Python.
-
wrote on 13 May 2019, 23:31 last edited by U7Development
Thanks for your reply....
Yeah, I briefly have seen MVC on Visual Studio, by the way, I guess (as you said) my project does not need it... for now....
Actually what I have is:
- A QMainWindow class for login interface
- A QMainWindow class for Master operations (adding, modifying and deleting users)
- A QMainWindow class for students
- A QMainWindow class for teachers
- A Custom struct that stores member functions that create db connections, queries, file operations and other misc.
Now that you mention MVC, I´m wondering for example, how do you separate View and Controller, while a push button is a GUI element but it has signals to program.. I see it like a mix... even buttons and forms are at the View side?
Thanks again!
-
wrote on 14 May 2019, 15:47 last edited by fcarney
Have the view delegate non gui logic to the controller. For instance, in a onClicked callback in QML don't have it do the business logic in that callback. Have it emit a signal with the relevant information. The signal would be connected to a controller object responsible for either the logic itself or delegating it to another part of the controller code. This layer of abstraction helps keep the pieces separate. It IS overhead though. This abstraction comes at a cost.
Ideally the view shouldn't know about the controller, but with the Qt signal/slot system at some point, some code somewhere, has to know about the slot object and the signal object to form a connection. I am guessing Qt has some ways of organizing things. I know they have the model/view paradigm and that might be enough.
I have a class I wrote to simulate pub/sub using signals and slots, but it is probably overkill. It was mainly written to reduce dependencies on static functions to make my code testable. Pub/sub is another method to abstract the interfaces from each other, but I don't know of a good off the shelf implementation for Qt.
Just think in terms of separation of concerns. What is the interface between the different pieces? What does each piece do? You don't have to divide up your program based upon MVC. You could come up with a different criteria as well. MVC was developed a long time ago to solve client/server communication issues over terminals if I remember correctly. So it may not fit your particular needs. I would do a search on design patterns on Google. I know I have read a few articles about many different patterns. Some I found useful, others I didn't really understand.
1/4