Model-View-ViewModel.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Model-View-ViewModel.cpp : Diese Datei enthält die Funktion "main". Hier beginnt und endet die Ausführung des Programms.
  2. // ChatGPT-4.0
  3. /*
  4. Model:
  5. Stores and manages the application data (data).
  6. Provides setData and getData methods to modify and retrieve the data.
  7. View:
  8. Responsible for interacting with the user.
  9. Displays data using displayData and shows messages using displayMessage.
  10. Accepts user input via the bindInputHandler method, which binds a callback function (from the ViewModel).
  11. ViewModel:
  12. Acts as a bridge between the Model and View.
  13. Updates the Model based on user input (onUserInput).
  14. Notifies the View of changes by calling the updateViewCallback (bound to the View’s update mechanism).
  15. Main:
  16. Sets up the relationships between the Model, View, and ViewModel.
  17. Binds the ViewModel’s update callback to the View’s displayData function.
  18. Binds the View’s input handler to the ViewModel’s onUserInput.
  19. How It Works:
  20. The user enters input through the View.
  21. The View forwards the input to the ViewModel using the bound callback.
  22. The ViewModel updates the Model and notifies the View of the changes.
  23. The View displays the updated data.
  24. Key Benefits:
  25. Separation of Concerns: The ViewModel encapsulates the logic, keeping the View and Model clean.
  26. Two-Way Binding: Changes in the Model automatically reflect in the View, and vice versa.
  27. Testability: The ViewModel can be tested independently of the View.
  28. */
  29. #include <iostream>
  30. #include <string>
  31. #include <functional>
  32. #include <memory>
  33. // Model
  34. class Model {
  35. private:
  36. std::string data;
  37. public:
  38. void setData(const std::string& newData) {
  39. data = newData;
  40. }
  41. std::string getData() const {
  42. return data;
  43. }
  44. };
  45. // View
  46. class View {
  47. public:
  48. void displayData(const std::string& data) const {
  49. std::cout << "Data: " << data << std::endl;
  50. }
  51. void displayMessage(const std::string& message) const {
  52. std::cout << message << std::endl;
  53. }
  54. void bindInputHandler(const std::function<void(const std::string&)>& handler) {
  55. std::string input;
  56. while (true) {
  57. std::cout << "Enter data (or 'quit' to exit): ";
  58. std::getline(std::cin, input);
  59. if (input == "quit") {
  60. break;
  61. }
  62. handler(input);
  63. }
  64. }
  65. };
  66. // ViewModel
  67. class ViewModel {
  68. private:
  69. Model& model;
  70. std::function<void(const std::string&)> updateViewCallback;
  71. public:
  72. ViewModel(Model& m) : model(m) {}
  73. void bindUpdateViewCallback(const std::function<void(const std::string&)>& callback) {
  74. updateViewCallback = callback;
  75. }
  76. void onUserInput(const std::string& input) {
  77. model.setData(input);
  78. if (updateViewCallback) {
  79. updateViewCallback(model.getData());
  80. }
  81. }
  82. std::string getData() const {
  83. return model.getData();
  84. }
  85. };
  86. // Main
  87. int main() {
  88. Model model;
  89. View view;
  90. ViewModel viewModel(model);
  91. // Bind the ViewModel's update callback to the View's display function
  92. viewModel.bindUpdateViewCallback([&view](const std::string& data) {
  93. view.displayData(data);
  94. });
  95. // Bind the View's input handler to the ViewModel's input handler
  96. view.bindInputHandler([&viewModel](const std::string& input) {
  97. viewModel.onUserInput(input);
  98. });
  99. return 0;
  100. }