How does the MVC framework for Desktop applications work?

Asked

Viewed 3,377 times

16

I have seen many web projects like php frameworks, Asp.net, however I read in some places that MVC came before the web, it was aimed at developing desktop applications, however I did not find material on the subject.

I am not talking about Web projects, I would like to know how it works in Desktop projects, mainly folder structure, however in desktop projects, mainly C++ I have no idea how this could be implemented.

How MVC should be implemented in a C++ project and how its folder structure?

  • I don’t quite understand, because apparently there is no MVC standard only for C++, I think you can adapt the same folder structure, these links may help you https://code.google.com/p/sydmvc/ and https://code.google.com/p/sydmvc/source/browse/#svn/trunk

  • Yes @Thiagofriedman does not only have a pattern for C++, as I said at the beginning of the question "I have seen many Web projects", currently exists for Web and for Cocoa (from apple), I could follow the pattern of Cocoa, but I really found it a little complex and I couldn’t quite understand it. The link problem you sent to me and with the documentation, there are the files. h, but it doesn’t explain how to use folder structure (if there is any structure in this project). Thanks anyway

  • Did any help you more? You need something to be improved?

  • 1

    @Moustache all helped me, only I didn’t choose because I couldn’t put in "practice" and point out what I think is most appropriate to my case. But I think your argument is quite correct "MVC does not define any of this. Everyone does MVC as they see fit"

3 answers

12

I cannot give a very complete answer now. But I can help with something.

This thing of organizing into folders and files is something that depends on the need. MVC does not define any of this. Each one does the MVC as well as understand. The standard exists to give a tool and not to impose a way of doing. It has several ways to get the same result by applying the standard.

I was bothered by that question and the answer also, but I do not know what to do with them so I eixei there. This is a cake recipe that does not lead anywhere, is not part of the MVC.

Often this separation is not even necessary. In others an even greater separation may be required, each component being in different projects. Few know that MVC shines brighter when it is done by different people in different projects. It’s common for people to follow patterns without wondering why they’re wearing it. MVC, like any other standard, does not solve problems that do not exist and it can create a new problem.

Look at the way you do Observer in Java and how it is done in C#. You can do it the same way in C# and now you can do it in a more convenient way in Java, but both implement the same pattern very differently.

Take a look at how the MVC using Qt (probably what else can help you).

In fact each one seems to have an insight into what this pattern actually is. Each framework implements and even interprets in a different way. Some end up changing the name a little, others use the name inappropriately, but since I am not an expert and I have never seen someone who guarantees what is and what is not MVC, I will not give my pinch (people who say they are experts and I have seen that they do not understand as much as they imagine).

Read more on article by Martin Fowler.

I don’t know how straight and organized it is but here is an example in C#. It’s not quite what you want but I think it helps.

What defines the pattern in its essence is that the data is in an object and they are dumb in relation to the rest, they only have behaviors related to the data (business rules) and not to the GUI, that is the model. The actions that the GUI must perform are received and processed by controller. It will take and manipulate the model’s data to suit the view needs. The view is that it processes the screen by receiving the data and sending commands to the controller.

In general the screen is mounted in a completely different way than what is done with web application. Of course, there are newer models that can work similarly to the web, sending a declarative code that will be rendered by some client, remote or embedded.

It is possible to do as on the web and have the model and controller and creation of the view on the server and the rendering of the view on the client (remote). This is not so simple to do, and although it may exist, I’ve never seen something ready and public to use this way (only a limitation of mine, nothing prevents and I believe it exists, I’m just saying that the common programmer will not know how to do).

But what I think is more common is to have all this in the client. The server serves only the pure data as the model in the client requests.

In some cases it is possible to have a hybrid form, perhaps with the model on the server and the controller and view on the client. There may not even be a server, eventually. Anyway, you can mount it in several ways.

An important concept that can help understand the need for MVC and see how it really applies in any type of GUI is the cohesion and coupling. The doubt probably comes from the lack of understanding of why the MVC exists.

Additional reading.

I’ve seen something about RUI and looks better than MVC. Early yet to state.

I can try to improve the answer if I have some extra details, but I think most things would be better answered in specific questions.

  • Yes this part of the Observer (or even the Dispatcher) that is something necessary for the structure to work (or not), I promise I will read and study your answer well.

  • This stitch is a side comment to show how it can vary. Not that it answers what you want :)

  • From what I am reading (and found so far) the basic is to create the Controller, View and Model "extensible" (would that be the word?), but there is no need and neither is the folder structure and neither "everything" of the application will use the MVC, as in the example with QT that you mentioned.

  • 1

    This you are correct. But as I said, it depends on the need. Almost every Isa has a question here about this folder thing as if it’s important for the default to work. This concern exists because people follow the recipe and do not question why they are actually using the standard. It has applications q have turned out very well without MVC. Even for web. I talk about Webpages here and most of them have never heard of it, let alone use: http://answall.com/a/49094/101 Some people use MVC for 1 page! I’ll never get tired of saying that people need to understand what they’re using.

  • Extensibility is something that is facilitated by MVC but needs to know how to do it. And it needs to be needed. Apply to the part that needs to use MVC. Apply other patterns that are relevant along with the MVC, do not adopt any pattern when none fits.

  • I fully agree with your comment (+1), however I believe that the use of MVC on the 1 page (home) would be due to the fact of the use of the "routes" what usually frameworks seem to me to be written for such routes to work directly with the controller or with an anonymous function that accesses some models and views. I think that in web when it comes to routes there is not much to avoid this, at most the controller can be avoided in the home page.

  • For simple things you don’t need a route. And there are ways to avoid it but we are already talking and something else. Unless you’re talking about SPA. I’m talking about 1 page even (SPA is a variable document, if it looks more like a desktop application). But it can be 10 pages, if it’s something very simple, almost static, that won’t be maintained much, won’t grow much, won’t have complexities, doesn’t need to organize too much. There are situations that the highest cost is the organization. Patterns should work to help solve problems and not be the problem.

  • http://answall.com/q/85901/101 Look, it’s a lot of silly concern with simple stuff. One thinks that organizing the project will make it better. For what I will create folders, files if I do a few simple things. I understand that because it is something of study one wants to do everything right. But people start to think that everything has to be like this. They only know how to follow the recipe. For bad luck or luck there is no (or had no) thing very ready for desktop, so people avoid abusing.

  • I understood it, I thought it was the first page, yes the onepage (and so you speak? ) are examples of situations that make sense to use MVC, since it is not something scalable (that would be the term? )... On the second comment (The extensibility is something that is facilitated by the MVC...), the need and trying to make scalable an application (I do not know if this is the term), but see thus, it is not a real need, only if one day I need to create a software where I will have access levels and need to reuse a model for example for different access levels (I do not know if this is the logic).

  • It depends, it may or may not make sense. It is that the SPA at the bottom are several pages in one document. It is like having several Forms in a window. Scalability can make sense if you think that the application can one day have a lot of screen, can change a lot, can have multiple actions, etc. It’s more complicated to apply MVC to simple things as much as not to apply to complex things. We’re talking about scaling the app, not her execution, right? I would need to see the concrete case but it may be that the extensibility is useful in this case.Since it ñ create the complexity that did not exist s/ solve anything

  • My previous comment was meaningless, I missed a "no" in the sentence and the meaning was backwards. I understood well, I read the term "classic mvc" and apparently it is very close to what you quoted (actually it is exactly what you quoted). Maybe really in this case what will be worth is the framework used, in my case I use QT a lot, but I rarely produce a "big software" to have the experience and see the need. I’ll run some more tests by the weekend and come back here. Thank you

  • I thought it was normal. I usually eat some things and it gets weird but I didn’t find it in this case, I don’t know. Interestingly his comment got weird :) But it gives p/ understand.

  • A question: "The MVC does not define any of this. Everyone does the MVC as they see fit." is their opinion or conceptual ?

Show 8 more comments

9

What is MVC

The definition of MVC has been evolving over time. In fact it has been greatly simplified.

In fact this term was born well before the WEB applications and today it is even difficult to say exactly what it was at that time. Martin Fowler, who was there to see, wrote what he remembers in this article: GUI Architectures.

The most basic vision one has of MVC currently is the clear separation of the concepts of presentation, logic of app and objects of domain in three different types of components (Model, View and Controller), each with a specific responsibility.

Domain objects*: objects representing the domain modeling and its business rules.

A graphical representation of this separation may be as follows:

W3Schools - ASP.NET MVC Tutorial

What this picture tells us is that controller knows the model and knows the view, who also knows the model, who in turn knows no one.

That is, it is paramount in the MVC that the model don’t know anyone!

Responsibility of each component in the MVC

The responsibilities of each component can be described as follows::

  • Model: Domain modeling - entities and other business objects.

  • View: Presentation of model for user interaction - forms or web pages.

  • Controller: Meets user requests, selects the model (for example an entity) and the view that the user will use to interact with the model.

Apart from the MVC, we can discuss a lot, as for example if the controller delivery the model in fact or whether one should always create a DTO, if the controller manipulates the model directly or uses a service facade if the controller must access the database, if the model is who is responsible for access to the bank or if neither of the two accesses the bank... But all this goes beyond the MVC, it is good not to confuse.

MVC may be associated with other Patterns design or architectural Patterns.

MVC alone cannot define the entire architecture and design of a system, especially a system that meets a complex domain.

Example of MVC implementation

The MVC seeks to find solution, for example, for the business rule codes that check the state of the controls on the form or web page (view) to make decisions.

Code example nay MVC:

class Formulario_Titulo {
    void baixar_parcelas_click() {
        sql = "select * from parcelas where titulo = :titulo";

        if checkBox_exclui_parcelas_a_vencer.Checked {
            sql += " and data_vencimento < hoje();"
        }

        parcelas = executa_sql(sql, textBox_titulo_id.Text);

        foreach(parcela in parcelas) {
            ... baixa a parcela
        }
    }
}

The above code is attached to the view (desktop form or web page), and it is very likely that it will be replicated when this routine is needed again elsewhere and this causes problems that we all know very well.

In a system that is MVC, the code above would turn something like this:

class Formulario_Titulo {
    botao_baixar_parcelas_onClick += patch();
}

class Titulo_Controller {
  void patch() {
    titulo.baixar_parcelas(textBox_titulo_id.Text, checkBox_exclui_titulos_a_vencer.Checked);
  }
}

class Titulo {
    void baixar_parcelas(String titulo_id, boolean exclui_titulos_a_vencer) {
        sql = "select * from parcelas where titulo = :titulo";

        if exclui_titulos_a_vencer {
            sql += " and data_vencimento < hoje();"
        }

        parcelas = executa_sql(sql, titulo_id);

        foreach(parcela in parcelas) {
            ... baixa a parcela
        }
    }
}

Then I started from a code that accumulated all the implementation in the form and divided the responsibilities into three components: Formulario_titles (view), Titulos_controller (controller, of course) and Title (model).

There are a thousand ways to implement MVC, depending on language, platform, frameworks, and other architectural and design decisions. The above example is just a pseudo-code to present a more concrete view of the concept.

Organisation of an MVC project

So far I tried to give a sense of what MVC is because it is no use trying to figure out how to organize an MVC project without having this notion.

The organization of the files and folders will also depend on the choice of language and frameworks that will be used - some frameworks have a suggested default organization that will make it easier to work with it, and you can still make your own framework or use any framework, setting its own standard.

For an idea, see the standard organization of Ruby On Rails (a framework for web applications based on Restfull and MVC):

In the examples, consider that the system provides a CRUD for an entity called Client.

    nome_app/
            app/
                views/
                    clientes/
                        index.html.erb
                        new.html.erb
                        show.html.erb
                controllers/
                    clientes_controller.rb
                models/
                    cliente.rb

See also the standard organization of ASP.NET MVC C# (very similar to Rails):

    Nome_App/
        Views/
            Cliente/
                Index.cshtml
                AlgumaViewEspecial.cshtml
                _AlgumaParcial.cshtml
        Controllers/
            ClienteController.cs
        Models/
            ClienteModels.cs

See also as one of my projects (Java JSF), using facelets, is organized:

    nome_app/
        /src/main/webapp/               (Views)
                    cliente/
                        index.xhtml
                        new.xhtml
                        show.xhtml
                        _algumTemplate.xhtml
        /src/main/java/                 
                    app/                (Controllers)
                        cliente/
                            Cliente_Controller.java
                    domain/ 
                        cliente/        (Model)
                            Cliente.java
                            ClienteRepo.java
                            Categoria.java

Note that in my Java structure I have mentioned in parentheses where each component of MVC is, as this may not be obvious in the nomenclature I use, since my nomenclature seeks to emphasize other aspects of architecture and not MVC itself (even this project using MVC). Eventually this structure will be even more complex if the complexity of the domain demands.

And there are many other folders in each project that meet other aspects and responsibilities of the system.

Completion

Once understanding the basic concept of MVC, its objective (to establish a clear separation between presentation and business responsibilities), the responsibility of each component and the relationship between them, you are free to choose the structure of your project or adapt to the structure suggested by the frameworks chosen if applicable.

And the fact that the application is Web or Desktop is not the overriding decision factor - you can base the structure of a Desktop project on any of these Web examples, the difference is that the view here is declared using Web library tags, and the Desktop app will be declared using desktop tools.

7

MVC is a pattern of object-oriented development. The application has at least one View class, which takes care exclusively of drawing and maintaining the screen. The Model class takes care of the "business rules" of the application, including talking to the database if any. (Some texts speak of the Model as if it were just a passive representation of the database, but I understand that this is wrong. The name of this is ORM - Object-Relational Mapping; the Model can make use of ORM but ORM alone cannot be called Model.)

The Controller is ideally a very simple class, a thin layer between Model and View. In a cross-platform application, the Model would tend to be easily portable, while View and Controller would have large dependencies on each platform and thus be specific to each platform.

So, if you do this separation into three classes in your C++ project, you’re following the MVC pattern.

Well, that’s ideal. In the real world, we end up deviating from that. A little because platforms encourage that. When you start an Android app, there is always an Activity, which sometimes does controller, but there is no Model template, so the difficult trend to avoid is to implement the application’s "intelligence" within Activity, and then it becomes difficult to separateI was going to reuse somewhere else. The same happens on iOS, the descending class of Uiviewcontroller is already there and ends up receiving all the non-visual code as well, although the original idea was that the Controller would only help the Views in what they cannot do alone, and compatible with the Model. (At least the View classes are clearly delimited on these platforms, so promiscuity occurs more between Model and Controller.)

  • can be honest, the answer is not bad, but it really became vague of "how to do", if I understood what you said, To say that I need 3 classes "extensible", then from them I will create for example the controllerfoo. h and controllerfoo.cpp which are extended from the controller. h (just one example), viewfoo. h and viewfoo.cpp which are extended from the view. h and modelfoo. h and modelfoo.cpp which are extended from the model. h (probably what generates the widget). What was not very clear is how to interact foocontroller.cpp with others. But it still seems to me a promising response.

  • Yes, there is a freedom of class layout. It is not mandatory to establish a hierarchy starting with abstract classes. That being said, Views usually have a superclass because there is a contain-is relationship between, for example, the screen view and the views of the controls.

  • Following his comment, "foocontroller" is who knows both the Model and the View. The Controller can even own the Model and View objects, so the destruction of the controller is the one that removes the other two objects.

  • You can do MVC without classes :)

  • In the case of applications with C++ that have Widgets they do have Classes, an example QT that classes are extended from Qwidget and are associated with the template file (which is an xml)

  • But you can do MVC without. I’m not saying you should or that a specific implementation gives.

  • I do not know if the last comment was for me @bigown, actually my comment would be just an addition to your comment, can be done without classes, as in QT we use classes for widgets generally :)

  • I would just leave out the part where the Model talks to the bank because, as you put it yourself, the Model is ideally uncoupled from the other components. Anyway I think it is a very elucidative and correct answer. + 1

  • 1

    @Bigown in fact can be done in C, in Assembler... do not use MVC or even object orientation "for beautiful". The intention of MVC is for you to have a portable and clearly separated Model from the View.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.