Git Product home page Git Product logo

mvc-example's Introduction

Model View Controller C++ Example

This example is based on the Java MVC example available at http://www.tutorialspoint.com/design_pattern/mvc_pattern.htm

A Eclipse CDT project is included here. Just import the project after doing git clone.

The MVC implemented uses the Observer pattern in a similar way described in the following figure:

MVC and Observer patterns

mvc-example's People

Contributors

javism avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

mvc-example's Issues

Inversión de control en el patrón MVC

Hola Javi!

El código hereda una malinterpretación del patrón MVC que proviene del artículo original. Normalmente el controlador no tiene que conocer a la vista. Más aún, es importante que la vista no necesite conocer al controlador para actualizarse. De esta forma pueden instanciarse varias vistas y controladores que pueden operar independientemente. Para ello suele utilizarse en sistemas orientado a objetos lo que se llama inversión de control [1].

Esta inversión de control, en orientación a objetos, se implementa a menudo mediante el patrón observer [2]. En C++ podemos utilizar boost::signals2 [3] para implementar el patrón, aunque en casos sencillos es fácil implementarlo a mano. Por ejemplo, podemos cambiar el modelo de este repositorio de esta forma

class StudentModel {
  ...
  void set...(...) {
    ...
    notify();
  }

  using Listener = std::function<void(const StudentModel&)>;
  void connect(Listener listener) {
    if (listeners_.find(listener) != listeners_.end())
      throw std::runtime_error("Double connection");
    listeners_.push_back(listener);
  }

  void disconnect(Listener listener) {
    auto iter = listeners_.find(listener);
    if (iter == listeners_.end())
      throw std::runtime_error("Listener not connected");
    listeners_.erase(listener);
  }

private:
  void notify() const {
    for (const auto& listener : listeners_)
      listener(*this);
  }
  std::vector<Listener> listeners_;
}

De esta forma, en lugar de dar la vista a conocer al controlador, podemos bien dar el modelo a la vista, o incluso realizar la conexión desde fuera, por ejemplo, en main.cpp podriamos hacer algo asi:

  StudentModel model = ...;
  StudentView view;
  model.connect([] (const StudentModel& model) {
    view.printStudentDetails(model.name(), model.date());
  });

Por supuesto, en esto pueden realizarse diferentes variaciones. Por ejemplo, incluyendo notificaciones más granulares (i.e. una por atributo), o utilizando técnicas de diffing como las usadas por React.js o Angular.js o añadiendo cierta forma de transacciones.

[1] https://en.wikipedia.org/wiki/Inversion_of_control
[2] https://en.wikipedia.org/wiki/Observer_pattern
[3] http://www.boost.org/doc/libs/1_57_0/doc/html/signals2.html

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.