Advertisement
ZondaKeN

Untitled

Mar 1st, 2024 (edited)
830
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.63 KB | None | 0 0
  1. // main.rs
  2. #![allow(warnings)]
  3.  
  4. mod model;
  5. mod view;
  6. mod observer;
  7. mod event;
  8. mod controller;
  9.  
  10. use std::cell::RefCell;
  11. use std::rc::Rc;
  12.  
  13. use model::Model;
  14. use view::View;
  15. use controller::Controller;
  16.  
  17. fn main() {
  18.     let mut model = Model::new(42);
  19.  
  20.     let mut view = View::new();
  21.  
  22.     let mut controller = Controller::new(&mut model, &mut view);
  23.  
  24.     controller.run();
  25. }
  26.  
  27. // model.rs
  28. pub struct Model
  29. {
  30.     pub x: i32
  31. }
  32.  
  33. impl Model
  34. {
  35.     pub fn new(x: i32) -> Self
  36.     {
  37.         return Self { x };
  38.     }
  39. }
  40.  
  41. // view.rs
  42. use std::cell::RefCell;
  43. use std::rc::Rc;
  44. use read_input::InputBuild;
  45. use read_input::prelude::input;
  46.  
  47. use crate::model::Model;
  48. use crate::event::Event;
  49.  
  50. pub struct View
  51. {
  52.     pub input_submitted: Event<i32>
  53. }
  54.  
  55. impl View
  56. {
  57.     pub fn new() -> Self
  58.     {
  59.         return Self
  60.         {
  61.             input_submitted: Event::<i32>::new()
  62.         };
  63.     }
  64.  
  65.     pub fn render(&self, model: &Model)
  66.     {
  67.         println!();
  68.         println!("Render... (x = {})", &model.x);
  69.         let input: i32 = input().repeat_msg("Input = ").get();
  70.         self.input_submitted.invoke(input);
  71.     }
  72. }
  73.  
  74. // controller.rs
  75. use std::cell::RefCell;
  76. use std::rc::Rc;
  77. use crate::model::Model;
  78. use crate::observer::Observer;
  79. use crate::view::View;
  80.  
  81. pub struct Controller<'a>
  82. {
  83.    model: &'a mut Model,
  84.     view: &'a View,
  85. }
  86.  
  87. impl<'a> Controller<'a>
  88. {
  89.    pub fn new(model: &'a mut Model, view: &'a mut View, x: impl Default) -> Self
  90.    {
  91.        let observer = ViewObserver { model };
  92.        view.input_submitted.subscribe(Box::new(observer));
  93.        let controller = Controller { model, view };
  94.        return controller;
  95.    }
  96.  
  97.    pub fn run(&mut self)
  98.    {
  99.        loop
  100.        {
  101.            self.view.render(self.model);
  102.        }
  103.    }
  104. }
  105.  
  106. struct ViewObserver<'a>
  107. {
  108.     model: &'a mut Model
  109. }
  110.  
  111. impl Observer<i32> for ViewObserver<'_>
  112. {
  113.     fn invoke(&self, arg1: i32)
  114.     {
  115.         println!("Input received (x = {})", arg1);
  116.     }
  117. }
  118.  
  119. // event.rs
  120. use crate::observer::Observer;
  121.  
  122. pub struct Event<T1>
  123. {
  124.     subscribers: Vec<Box<dyn Observer<T1>>>
  125. }
  126.  
  127. impl<T1> Event<T1> where T1 : Clone
  128. {
  129.     pub fn new() -> Self
  130.     {
  131.         return Self
  132.         {
  133.             subscribers: Vec::new()
  134.         };
  135.     }
  136.  
  137.     pub fn subscribe(&mut self, arg: Box<dyn Observer<T1>>)
  138.     {
  139.         self.subscribers.push(arg);
  140.     }
  141.  
  142.     pub fn invoke(&self, arg1: T1)
  143.     {
  144.         for subscriber in &self.subscribers
  145.         {
  146.             subscriber.invoke(arg1.clone());
  147.         }
  148.     }
  149. }
  150.  
  151. // observer.rs
  152. pub trait Observer<T1>
  153. {
  154.     fn invoke(&self, arg1: T1);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement