Created
August 1, 2022 21:04
-
-
Save ilopX/960554e2dbace375c1d6a1609f758703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(trait_upcasting)] | |
| #![allow(incomplete_features)] | |
| extern crate core; | |
| use std::borrow::Borrow; | |
| use std::thread::sleep; | |
| use std::time::Duration; | |
| use crate::gui_app::{Form, GuiFactory}; | |
| use crate::win::{WinFactory}; | |
| use crate::mac::{MacFactory}; | |
| fn main() { | |
| for factory_name in ["win", "mac"] { | |
| let factory = get_factory(factory_name); | |
| let form = Form::new(factory.borrow()); | |
| form.render(); | |
| } | |
| sleep(Duration::from_millis(20_000)); | |
| } | |
| fn get_factory(name: &str) -> Box<dyn GuiFactory> { | |
| match name { | |
| "win" => Box::new(WinFactory), | |
| "mac" => Box::new(MacFactory), | |
| _ => { | |
| panic!("factory not found."); | |
| } | |
| } | |
| } | |
| pub mod gui_app { | |
| pub struct Form { | |
| component: Vec<Box<dyn Component>>, | |
| } | |
| impl Form { | |
| pub fn new(gui_factory: &dyn GuiFactory) -> Self { | |
| Self { | |
| component: vec![ | |
| gui_factory.create_button(), | |
| gui_factory.create_checkbox(), | |
| ] | |
| } | |
| } | |
| pub fn render(&self) { | |
| for cmp in &self.component { | |
| cmp.draw(); | |
| } | |
| } | |
| } | |
| pub trait Component { | |
| fn draw(&self); | |
| } | |
| pub trait Button: Component {} | |
| pub trait Checkbox: Component {} | |
| pub trait GuiFactory { | |
| fn create_button(&self) -> Box<dyn Button>; | |
| fn create_checkbox(&self) -> Box<dyn Checkbox>; | |
| } | |
| } | |
| pub mod win { | |
| use crate::gui_app::{Button, Checkbox, Component}; | |
| use crate::GuiFactory; | |
| pub struct WinFactory; | |
| impl GuiFactory for WinFactory { | |
| fn create_button(&self) -> Box<dyn Button> { | |
| Box::new(WinButton {}) | |
| } | |
| fn create_checkbox(&self) -> Box<dyn Checkbox> { | |
| Box::new(WinCheckbox {}) | |
| } | |
| } | |
| struct WinButton; | |
| impl Component for WinButton { | |
| fn draw(&self) { | |
| println!("Draw WinButton"); | |
| } | |
| } | |
| impl Button for WinButton {} | |
| struct WinCheckbox; | |
| impl Component for WinCheckbox { | |
| fn draw(&self) { | |
| println!("Draw WinCheckbox"); | |
| } | |
| } | |
| impl Checkbox for WinCheckbox {} | |
| } | |
| pub mod mac { | |
| use crate::gui_app::{Button, Checkbox, Component}; | |
| use crate::GuiFactory; | |
| pub struct MacFactory; | |
| impl GuiFactory for MacFactory { | |
| fn create_button(&self) -> Box<dyn Button> { | |
| Box::new(MacButton) | |
| } | |
| fn create_checkbox(&self) -> Box<dyn Checkbox> { | |
| Box::new(MacCheckbox) | |
| } | |
| } | |
| struct MacButton; | |
| impl Component for MacButton { | |
| fn draw(&self) { | |
| println!("Draw MacButton"); | |
| } | |
| } | |
| impl Button for MacButton {} | |
| struct MacCheckbox; | |
| impl Component for MacCheckbox { | |
| fn draw(&self) { | |
| println!("Draw MacCheckbox"); | |
| } | |
| } | |
| impl Checkbox for MacCheckbox {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment