| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- // Model-View-ViewModel.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
- // ChatGPT-4.0
- /*
- Model:
- Stores and manages the application data (data).
- Provides setData and getData methods to modify and retrieve the data.
- View:
- Responsible for interacting with the user.
- Displays data using displayData and shows messages using displayMessage.
- Accepts user input via the bindInputHandler method, which binds a callback function (from the ViewModel).
- ViewModel:
- Acts as a bridge between the Model and View.
- Updates the Model based on user input (onUserInput).
- Notifies the View of changes by calling the updateViewCallback (bound to the View’s update mechanism).
- Main:
- Sets up the relationships between the Model, View, and ViewModel.
- Binds the ViewModel’s update callback to the View’s displayData function.
- Binds the View’s input handler to the ViewModel’s onUserInput.
- How It Works:
- The user enters input through the View.
- The View forwards the input to the ViewModel using the bound callback.
- The ViewModel updates the Model and notifies the View of the changes.
- The View displays the updated data.
- Key Benefits:
- Separation of Concerns: The ViewModel encapsulates the logic, keeping the View and Model clean.
- Two-Way Binding: Changes in the Model automatically reflect in the View, and vice versa.
- Testability: The ViewModel can be tested independently of the View.
- */
- #include <iostream>
- #include <string>
- #include <functional>
- #include <memory>
- // Model
- class Model {
- private:
- std::string data;
- public:
- void setData(const std::string& newData) {
- data = newData;
- }
- std::string getData() const {
- return data;
- }
- };
- // View
- class View {
- public:
- void displayData(const std::string& data) const {
- std::cout << "Data: " << data << std::endl;
- }
- void displayMessage(const std::string& message) const {
- std::cout << message << std::endl;
- }
- void bindInputHandler(const std::function<void(const std::string&)>& handler) {
- std::string input;
- while (true) {
- std::cout << "Enter data (or 'quit' to exit): ";
- std::getline(std::cin, input);
- if (input == "quit") {
- break;
- }
- handler(input);
- }
- }
- };
- // ViewModel
- class ViewModel {
- private:
- Model& model;
- std::function<void(const std::string&)> updateViewCallback;
- public:
- ViewModel(Model& m) : model(m) {}
- void bindUpdateViewCallback(const std::function<void(const std::string&)>& callback) {
- updateViewCallback = callback;
- }
- void onUserInput(const std::string& input) {
- model.setData(input);
- if (updateViewCallback) {
- updateViewCallback(model.getData());
- }
- }
- std::string getData() const {
- return model.getData();
- }
- };
- // Main
- int main() {
- Model model;
- View view;
- ViewModel viewModel(model);
- // Bind the ViewModel's update callback to the View's display function
- viewModel.bindUpdateViewCallback([&view](const std::string& data) {
- view.displayData(data);
- });
- // Bind the View's input handler to the ViewModel's input handler
- view.bindInputHandler([&viewModel](const std::string& input) {
- viewModel.onUserInput(input);
- });
- return 0;
- }
|