From 2c51480ee4564e5f99175f3d8d2bda5960ed8f24 Mon Sep 17 00:00:00 2001 From: lpxxn Date: Wed, 26 Feb 2020 15:33:56 +0800 Subject: [PATCH] factory --- README.md | 1 + creational/abstract_factory.rs | 89 ++++++++++++++++++++++++++++++++++ creational/factory.rs | 4 +- simple_factory_pattern.rs | 1 - 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 creational/abstract_factory.rs delete mode 100644 simple_factory_pattern.rs diff --git a/README.md b/README.md index 7e52c4f..3078db0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/creational/abstract_factory.rs b/creational/abstract_factory.rs new file mode 100644 index 0000000..430c16e --- /dev/null +++ b/creational/abstract_factory.rs @@ -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; + fn create_checkbox(&self) -> Box; +} + +struct WinFactory; +impl GUIFactory for WinFactory { + fn create_button(&self) -> Box { + Box::new(WinButton {}) + } + fn create_checkbox(&self) -> Box { + Box::new(WinCheckbox {}) + } +} + +struct MacFactory; +impl GUIFactory for MacFactory { + fn create_button(&self) -> Box { + Box::new(MacButton {}) + } + fn create_checkbox(&self) -> Box { + 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 { + 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 +} diff --git a/creational/factory.rs b/creational/factory.rs index ed41381..372801e 100644 --- a/creational/factory.rs +++ b/creational/factory.rs @@ -23,7 +23,7 @@ struct Circl {} impl Shap for Circl { fn draw(&self) { - println!("draw a circl !"); + println!("draw a circl!"); } } @@ -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! diff --git a/simple_factory_pattern.rs b/simple_factory_pattern.rs deleted file mode 100644 index 8b13789..0000000 --- a/simple_factory_pattern.rs +++ /dev/null @@ -1 +0,0 @@ -