@@ -5,18 +5,40 @@ without specifying their concrete classes._
55
66## GUI Factory Example
77
8- There is a GUI Factory trait and two implementations: Windows and Mac OS factories. A factory can create buttons and checkboxes, however, depending on
9- thr factory subtype, it will create either Windows or Mac OS components.
8+ This example shows a GUI framework can organize its classes into independent
9+ libraries:
10+
11+ 1 . The ` gui ` library defines interfaces for all the components.
12+ It has no external dependencies.
13+ 2 . The ` windows ` library provides Windows implementation of the base GUI.
14+ Depends on ` gui ` .
15+ 3 . The ` macos ` library provides Mac OS implementation of the base GUI.
16+ Depends on ` gui ` .
17+
18+ The ` app ` is a client application that can use several implementations of the
19+ GUI framework, depending on the current environment or configuration.
20+ However, most of the ` app ` code _ ** doesn't depend on specific types of GUI
21+ elements** _ . All the client code works with GUI elements through abstract
22+ interfaces (traits) defined by the ` gui ` lib.
1023
1124There are also 2 approaches to implementing abstract factory in Rust:
12- using generics and using dynamic allocation.
25+ using generics (_ static dispatch_ ) and using dynamic allocation
26+ (_ dynamic dispatch_ ).
1327
14- ## ` main.rs `
28+ ## ` app/ main.rs`
1529
1630Here, abstract factory is implemented via ** generics** which allow the compiler
17- to create a code that doesn't require dynamic dispatch in runtime.
31+ to create a code that does NOT require dynamic dispatch in runtime.
1832
19- See ` GuiFactory ` in [ gui/mod.rs] ( gui/mod.rs ) .
33+ ``` rust
34+ pub trait GuiFactory {
35+ type B : Button ;
36+ type C : Checkbox ;
37+
38+ fn create_button (& self ) -> Self :: B ;
39+ fn create_checkbox (& self ) -> Self :: C ;
40+ }
41+ ```
2042
2143### How to Run
2244
@@ -33,12 +55,17 @@ Windows checkbox has switched
3355Windows checkbox has switched
3456```
3557
36- ## ` main_dyn.rs `
58+ ## ` app/ main_dyn.rs`
3759
3860If a concrete type of abstract factory is not known at the compilation time,
3961then is should be implemented using ` Box ` pointers.
4062
41- See See ` GuiFactoryDynamic ` in [ gui/mod.rs] ( gui/mod.rs ) .
63+ ``` rust
64+ pub trait GuiFactoryDynamic {
65+ fn create_button (& self ) -> Box <dyn Button >;
66+ fn create_checkbox (& self ) -> Box <dyn Checkbox >;
67+ }
68+ ```
4269
4370### How to Run
4471
0 commit comments