Skip to content

Commit f8497c1

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 f8497c1

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
}

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: 4 additions & 4 deletions
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 {
@@ -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> {
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::MagicMaze;
6+
use ordinary_maze::OrdinaryMaze;
127

138
fn 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
}

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

Lines changed: 4 additions & 4 deletions
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 {
@@ -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> {

0 commit comments

Comments
 (0)