Skip to content

Commit

Permalink
factory
Browse files Browse the repository at this point in the history
  • Loading branch information
lpxxn committed Feb 26, 2020
1 parent 7f83ed5 commit 2c51480
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
| Pattern | Description | Status |
|:-------:|:----------- |:------:|
| [Factory Method](/creational/factory.rs) | Defers instantiation of an object to a specialized function for creating instances ||
| [Abstract Factory](/creational/abstract_factory.rs) | Provides an interface for creating families of releated objects ||


## Behavioral Patterns
Expand Down
89 changes: 89 additions & 0 deletions creational/abstract_factory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
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
}
4 changes: 2 additions & 2 deletions creational/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Circl {}

impl Shap for Circl {
fn draw(&self) {
println!("draw a circl !");
println!("draw a circl!");
}
}

Expand All @@ -39,7 +39,7 @@ impl ShapFactory {

fn main() {
let shap = ShapFactory::new_shap(&ShapType::Circl);
shap.draw(); // output: draw a circl !
shap.draw(); // output: draw a circl!

let shap = ShapFactory::new_shap(&ShapType::Rectangle);
shap.draw(); // output: draw a rectangle!
Expand Down
1 change: 0 additions & 1 deletion simple_factory_pattern.rs

This file was deleted.

0 comments on commit 2c51480

Please sign in to comment.