File tree Expand file tree Collapse file tree 5 files changed +43
-44
lines changed
creational/factory-method/maze-game Expand file tree Collapse file tree 5 files changed +43
-44
lines changed Original file line number Diff line number Diff line change 1+ /// Maze room that is going to be instantiated with a factory method.
2+ pub trait Room {
3+ fn render ( & self ) ;
4+ }
5+
6+ /// Maze game has a factory method producing different rooms.
7+ pub trait MazeGame {
8+ type RoomImpl : Room ;
9+
10+ /// A factory method.
11+ fn rooms ( & self ) -> Vec < Self :: RoomImpl > ;
12+
13+ fn play ( & self ) {
14+ for room in self . rooms ( ) {
15+ room. render ( ) ;
16+ }
17+ }
18+ }
19+
20+ /// The client code initializes resources and does other preparations
21+ /// then it uses a factory to construct and run the game.
22+ pub fn run ( maze_game : impl MazeGame ) {
23+ println ! ( "Loading resources..." ) ;
24+ println ! ( "Starting the game..." ) ;
25+
26+ maze_game. play ( ) ;
27+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1- use super :: { MazeGame , Room } ;
1+ use super :: game :: { MazeGame , Room } ;
22
33#[ derive( Clone ) ]
44pub struct MagicRoom {
@@ -17,11 +17,11 @@ impl Room for MagicRoom {
1717 }
1818}
1919
20- pub struct MagicMazeGame {
20+ pub struct MagicMaze {
2121 rooms : Vec < MagicRoom > ,
2222}
2323
24- impl MagicMazeGame {
24+ impl MagicMaze {
2525 pub fn new ( ) -> Self {
2626 Self {
2727 rooms : vec ! [
@@ -32,7 +32,7 @@ impl MagicMazeGame {
3232 }
3333}
3434
35- impl MazeGame for MagicMazeGame {
35+ impl MazeGame for MagicMaze {
3636 type RoomImpl = MagicRoom ;
3737
3838 fn rooms ( & self ) -> Vec < Self :: RoomImpl > {
Original file line number Diff line number Diff line change 11mod game;
2+ mod magic_maze;
3+ mod ordinary_maze;
24
3- /// The client code demonstrates that it does its own things
4- /// (e.g. loading resources) and it can use a factory in a proper place to
5- /// construct a game.
6- fn run ( game : impl game:: MazeGame ) {
7- println ! ( "Loading resources..." ) ;
8- println ! ( "Starting the game..." ) ;
9-
10- game. play ( ) ;
11- }
5+ use magic_maze:: MagicMaze ;
6+ use ordinary_maze:: OrdinaryMaze ;
127
138fn main ( ) {
14- let game = game :: MagicMazeGame :: new ( ) ;
15- run ( game ) ;
9+ let magic_maze = MagicMaze :: new ( ) ;
10+ game :: run ( magic_maze ) ;
1611
17- let game = game :: OrdinaryMazeGame :: new ( ) ;
18- run ( game ) ;
12+ let ordinary_maze = OrdinaryMaze :: new ( ) ;
13+ game :: run ( ordinary_maze ) ;
1914}
Original file line number Diff line number Diff line change 1- use super :: { MazeGame , Room } ;
1+ use super :: game :: { MazeGame , Room } ;
22
33#[ derive( Clone ) ]
44pub struct OrdinaryRoom {
@@ -17,19 +17,19 @@ impl Room for OrdinaryRoom {
1717 }
1818}
1919
20- pub struct OrdinaryMazeGame {
20+ pub struct OrdinaryMaze {
2121 rooms : Vec < OrdinaryRoom > ,
2222}
2323
24- impl OrdinaryMazeGame {
24+ impl OrdinaryMaze {
2525 pub fn new ( ) -> Self {
2626 Self {
2727 rooms : vec ! [ OrdinaryRoom :: new( 1 ) , OrdinaryRoom :: new( 2 ) ] ,
2828 }
2929 }
3030}
3131
32- impl MazeGame for OrdinaryMazeGame {
32+ impl MazeGame for OrdinaryMaze {
3333 type RoomImpl = OrdinaryRoom ;
3434
3535 fn rooms ( & self ) -> Vec < Self :: RoomImpl > {
You can’t perform that action at this time.
0 commit comments