hey guyz..
I don't understand the REAL job of a controller in MVC pattern.
Can anyone show me a simple program with the controller's code?

thanks a lot

Basic program coming up... please forgive the datatypes, I work mainly with QT libraries

MODEL:

QTreeWidgetItem ContainerClass::loadData(){

    <Database call and query>

    QTreeWidgetItem item.text(<field index> = query.record.value(<fieldname>).toString().trimmed();

    return item;

}

VIEW:

void ViewClass::on_btnLoad_clicked(){

    ControllerObject * TWControlObject = new ControllerObject()

    TWControlObject->loadData();
    
}

Controller:

void ControllerObject::loadData(){

    ContainerClass * DataObject = new ContainerClass();

    tw->addTopLevelItem(DataObject->loadData());

}

Sorry but i don't understand this :/
Let me explain what i am looking for..
For example, we have a Form (like the form.bmp) -look down-.
Also, we have a database to store the data.
When the user input data on editbox and press the insert button, the controller take effect and do the job, i'm answering what is his job?


example:

we have a Person class.

class Person{
       private:
             String _name;
       public:
             void setname(String name){...}

And a PersonDB class

class PersonDB:public Person{
            bool AddpersonTodb(Person* person){//it creates a person and add it to db}

I have read that the controller is something like mediator between View (Form.bmp) and Model (Person class), but i don't know what is the REAL job of controller, for example, it calls the PersonDB methods? and how update/change the state of a class?

I'm totally confused :(

In this case I would not use "Person* person" in the Model,

I would have a Controller class,

which will be instantiated as an object in your View class (Person)

the Controller class will have an object for the Model class (PersonDB)...

and you will work with a middle method to call the functionality of the Model, Or you can even try something like the following in the view itself:

void Person::saveData(){

    <ControllerName>.PersonDB.AddpersonTodb(Person name)
}

If I try to explain it... the Controller communicates the program functionality (in the Model) with the user input handled in the View

The Wikipedia article explains it pretty well.

The essential idea is that your application's parts should not need to know too much about each other.

For example, let's consider a simple text editor.

The "model" represents the textual data and all the stuff you can do to it, like load and save to file, search it and replace text, add and delete text, etc.

The "view" is responsible for displaying the text to the user and the UI elements that the user manipulates to modify it.

The "controller" manages these two things. For example, if the view signals that the user wishes to save (because he pressed Ctrl+S), the controller will do things like check to see if the document needs saving, whether the user needs to be asked for a filename, etc, before instructing the model to save, and afterward reporting any potential failure.

Typically an application will have more than one view (a text editor has other GUI elements besides the text widget, like menus, etc). Often the view and controller are treated as the same thing as well... It is also possible for the view to directly manipulate the model as well. (For example, pressing the Enter key when the text widget has focus doesn't necessarily involve controller action.)

Ideally, this can all be done in such a way as to make the exact model or view changeable to a different one. (For example, you could swap the view from a plain text editor to a WYSIWYG HTML view.)

Sorry for the typos -- sleeping baby on one arm...
Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.