Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(warnings)]
- use std::cell::Cell;
- pub struct Model
- {
- x: i32
- }
- pub struct View<'a> {
- model: &'a Cell<Model>,
- }
- pub struct Controller<'a> {
- model: &'a Cell<Model>,
- view: &'a View<'a>,
- }
- impl View<'_>
- {
- pub fn render(&self)
- {
- println!("Render...");
- }
- }
- trait Observer<T1>
- {
- fn invoke(&self, arg1: T1);
- }
- struct Event<T1>
- {
- subscribers: Vec<Box<dyn Observer<T1>>>
- }
- impl<T1> Event<T1>
- {
- fn subscribe(&mut self, arg: impl Observer<T1> + 'static)
- {
- self.subscribers.push(Box::new(arg));
- }
- fn invoke(&self, arg1: T1) where T1 : Clone
- {
- for subscriber in &self.subscribers
- {
- subscriber.invoke(arg1.clone());
- }
- }
- }
- impl<'a> Controller<'a>
- {
- pub fn new(model: &'a Cell<Model>, view: &'a View<'a>) -> Self
- {
- let controller = Controller { model, view };
- //view.add_observer(ViewObserver {});
- return controller;
- }
- pub fn run(&mut self)
- {
- self.view.render();
- }
- }
- fn main() {
- let model = Cell::new(Model { x: 42 });
- let view = View { model: &model };
- let mut controller = Controller::new(&model, &view);
- controller.run();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement