Advertisement
ZondaKeN

Untitled

Feb 29th, 2024
1,052
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.25 KB | None | 0 0
  1. #![allow(warnings)]
  2.  
  3. use std::cell::Cell;
  4.  
  5. pub struct Model
  6. {
  7.     x: i32
  8. }
  9.  
  10. pub struct View<'a> {
  11.    model: &'a Cell<Model>,
  12. }
  13.  
  14. pub struct Controller<'a> {
  15.    model: &'a Cell<Model>,
  16.     view: &'a View<'a>,
  17. }
  18.  
  19. impl View<'_>
  20. {
  21.    pub fn render(&self)
  22.    {
  23.        println!("Render...");
  24.    }
  25. }
  26.  
  27. trait Observer<T1>
  28. {
  29.    fn invoke(&self, arg1: T1);
  30. }
  31.  
  32. struct Event<T1>
  33. {
  34.    subscribers: Vec<Box<dyn Observer<T1>>>
  35. }
  36.  
  37. impl<T1> Event<T1>
  38. {
  39.    fn subscribe(&mut self, arg: impl Observer<T1> + 'static)
  40.     {
  41.         self.subscribers.push(Box::new(arg));
  42.     }
  43.  
  44.     fn invoke(&self, arg1: T1) where T1 : Clone
  45.     {
  46.         for subscriber in &self.subscribers
  47.         {
  48.             subscriber.invoke(arg1.clone());
  49.         }
  50.     }
  51. }
  52.  
  53. impl<'a> Controller<'a>
  54. {
  55.     pub fn new(model: &'a Cell<Model>, view: &'a View<'a>) -> Self
  56.    {
  57.        let controller = Controller { model, view };
  58.        //view.add_observer(ViewObserver {});
  59.        return controller;
  60.    }
  61.  
  62.    pub fn run(&mut self)
  63.    {
  64.        self.view.render();
  65.    }
  66. }
  67.  
  68. fn main() {
  69.    let model = Cell::new(Model { x: 42 });
  70.    let view = View { model: &model };
  71.    let mut controller = Controller::new(&model, &view);
  72.    controller.run();
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement