Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract Factory #225

Open
Tracked by #116
simonsan opened this issue Feb 22, 2021 · 3 comments
Open
Tracked by #116

Abstract Factory #225

simonsan opened this issue Feb 22, 2021 · 3 comments
Labels
A-pattern Area: Content about Patterns C-addition Category: Adding new content, something that didn't exist in the repository before C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content
Projects

Comments

@simonsan
Copy link
Collaborator

Tracking issue for merging: https://github.com/lpxxn/rust-design-pattern/blob/master/creational/abstract_factory.rs

Example:

//! Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.

trait GUIFactory {
    fn create_button(&self) -> Box<dyn Button>;
    fn create_checkbox(&self) -> Box<dyn Checkbox>;
}

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 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 {})
    }
}

trait Button {
    fn paint(&self);
}

struct WinButton;
impl Button for WinButton {
    fn paint(&self) {
        println!("windows os button");
    }
}

struct MacButton;
impl Button for MacButton {
    fn paint(&self) {
        println!("mac os button");
    }
}

trait Checkbox {
    fn paint(&self);
}

struct WinCheckbox;
impl Checkbox for WinCheckbox {
    fn paint(&self) {
        println!("windows os checkbox");
    }
}

struct MacCheckbox;
impl Checkbox for MacCheckbox {
    fn paint(&self) {
        println!("mac os checkbox");
    }
}

struct Application;
impl Application {
    fn new_gui_factory(os: &str) -> Box<dyn GUIFactory> {
        match os {
            "mac" => Box::new(MacFactory {}),
            "win" => Box::new(WinFactory {}),
            _ => panic!("error"),
        }
    }
}

fn main() {
    let mac_app = Application::new_gui_factory("mac");
    let btn = mac_app.create_button();
    btn.paint(); // output: mac os button
    let cb = mac_app.create_checkbox();
    cb.paint(); // output: mac os checkbox

    let win_app = Application::new_gui_factory("win");
    let btn = win_app.create_button();
    btn.paint(); // output: windows os button
    let cb = win_app.create_checkbox();
    cb.paint(); // output: windows os checkbox
}
@simonsan simonsan added C-addition Category: Adding new content, something that didn't exist in the repository before A-pattern Area: Content about Patterns labels Feb 22, 2021
@simonsan
Copy link
Collaborator Author

Tracking discussion on merging: lpxxn/rust-design-pattern#7

@simonsan simonsan added this to ToDo-Patterns in Content Feb 22, 2021
@pickfire
Copy link
Contributor

The provided example does not seemed ergonomic, for good GUI design we should considered looking at druid.

@simonsan
Copy link
Collaborator Author

Do you have a code example for that, we are still collecting information here, so nothing put into stone.

@simonsan simonsan added the C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content label Feb 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-pattern Area: Content about Patterns C-addition Category: Adding new content, something that didn't exist in the repository before C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content
Projects
Content
ToDo-Patterns
Development

No branches or pull requests

2 participants