-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.rs
119 lines (106 loc) · 3.18 KB
/
builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#![allow(dead_code)]
/**
* -----------------------------------------------------------------------------
* BUILDER PATTERN
*
* To execute, please run: cargo run --bin builder
* To run tests, please run: cargo test --bin builder
* -----------------------------------------------------------------------------
*
*
* Builder Pattern lets us construct objects step by step. As rustlang does not
* support object-oriented programming, We can not build different type of
* object with the help of inheritance, however we can construct structures with
* optional values.
*
* The example below shows a builder pattern that builds computer with processor
* and memory with optional
* Keyboard and Display.
**/
#[derive(Debug)]
enum Processor {
AMD,
ARM,
Intel,
}
#[derive(Debug)]
enum KeyboardVariant {
Backlit,
NonBacklit,
Mechanical,
}
#[derive(Debug)]
enum Display {
HD,
FHD,
QHD,
UHD,
}
#[derive(Debug)]
struct Computer {
processor: Processor,
// all optional values will be build after instantiation
memory: Option<u32>,
keyboard: Option<KeyboardVariant>,
display: Option<Display>,
}
impl Computer {
fn new(processor: Processor) -> Self {
Self {
processor,
memory: None,
keyboard: None,
display: None,
}
}
fn set_ram(&mut self, size_mb: u32) {
self.memory = Some(size_mb);
}
fn set_display(&mut self, variant: Display) {
self.display = Some(variant);
}
fn set_keyboard(&mut self, variant: KeyboardVariant) {
self.keyboard = Some(variant)
}
fn boot(&self) {
match self.memory {
Some(size) => println!(
"The computer boots with {:?} processor and {size}MB of memory",
self.processor
),
None => println!("The Computer failed to boot!! CODE: NO_MEMORY_FOUND"),
}
}
fn input(&self, key: char) {
match &self.keyboard {
Some(kbd) => println!("Key: '{key}' pressed on the {:?} keyboard", kbd),
None => println!("No Keyboard found !!"),
}
}
fn display_resolution(&self) {
match self.display {
Some(Display::HD) => println!("DISPLAY RESOLUTION IS: 1366 X 768"),
Some(Display::FHD) => println!("DISPLAY RESOLUTION IS: 1920 X 1080"),
Some(Display::QHD) => println!("DISPLAY RESOLUTION IS: 2560 X 1440"),
Some(Display::UHD) => println!("DISPLAY RESOLUTION IS: 3840 X 2160"),
None => println!("NO DISPLAY FOUND !!"),
}
}
}
fn main() {
// build Computer
let mut c = Computer::new(Processor::Intel);
println!("{:?}", c);
// set RAM
c.boot(); // The Computer failed to boot!! CODE: NO_MEMORY_FOUND
c.set_ram(4096);
c.boot(); // The computer boots with 4096MB of RAM
// attach keyboard
c.input('A'); // No Keyboard found !!
c.set_keyboard(KeyboardVariant::Mechanical);
c.input('A'); // 'A' pressed on the Mechanical keyboard
// attach monitor
c.display_resolution(); // NO DISPLAY FOUND !!
c.set_display(Display::FHD);
c.display_resolution(); // DISPLAY RESOLUTION IS: 1920 X 1080
}