Skip to content

Commit b754af0

Browse files
committed
Provide an alternative structure of Maze Game
The aim is to demonstrate a value of the Factory Method pattern. The Game trait (interface) should be decoupled modularly from impls. It would demonstrate that there could be many implementations, and the game runs similarly with different ones.
1 parent 92f9a62 commit b754af0

File tree

5 files changed

+38
-38
lines changed

5 files changed

+38
-38
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 demonstrates that it does its own things
21+
/// (e.g. loading resources) and it can use a factory in a proper place to
22+
/// construct a game.
23+
pub fn run(game: impl MazeGame) {
24+
println!("Loading resources...");
25+
println!("Starting the game...");
26+
27+
game.play();
28+
}

creational/factory-method/maze-game/game/mod.rs

Lines changed: 0 additions & 23 deletions
This file was deleted.

creational/factory-method/maze-game/game/magic_maze.rs renamed to creational/factory-method/maze-game/magic_maze.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{MazeGame, Room};
1+
use super::game::{MazeGame, Room};
22

33
#[derive(Clone)]
44
pub struct MagicRoom {
Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
mod 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::MagicMazeGame;
6+
use ordinary_maze::OrdinaryMazeGame;
127

138
fn main() {
14-
let game = game::MagicMazeGame::new();
15-
run(game);
9+
let magic_maze = MagicMazeGame::new();
10+
game::run(magic_maze);
1611

17-
let game = game::OrdinaryMazeGame::new();
18-
run(game);
12+
let ordinary_maze = OrdinaryMazeGame::new();
13+
game::run(ordinary_maze);
1914
}

creational/factory-method/maze-game/game/ordinary_maze.rs renamed to creational/factory-method/maze-game/ordinary_maze.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{MazeGame, Room};
1+
use super::game::{MazeGame, Room};
22

33
#[derive(Clone)]
44
pub struct OrdinaryRoom {

0 commit comments

Comments
 (0)