-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain1.cpp
98 lines (80 loc) · 2.4 KB
/
main1.cpp
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
#include <iostream>
#include <string>
// Product class
class Computer {
public:
void setCPU(const std::string& cpu) {
cpu_ = cpu;
}
void setMemory(const std::string& memory) {
memory_ = memory;
}
void setStorage(const std::string& storage) {
storage_ = storage;
}
void display() {
std::cout << "CPU: " << cpu_ << std::endl;
std::cout << "Memory: " << memory_ << std::endl;
std::cout << "Storage: " << storage_ << std::endl;
}
private:
std::string cpu_;
std::string memory_;
std::string storage_;
};
// Builder interface
class ComputerBuilder {
public:
virtual void buildCPU(const std::string& cpu) = 0;
virtual void buildMemory(const std::string& memory) = 0;
virtual void buildStorage(const std::string& storage) = 0;
virtual Computer getResult() = 0;
};
// Concrete Builder
class DesktopComputerBuilder : public ComputerBuilder {
public:
DesktopComputerBuilder() {
computer_ = Computer();
}
void buildCPU(const std::string& cpu) override {
computer_.setCPU(cpu);
}
void buildMemory(const std::string& memory) override {
computer_.setMemory(memory);
}
void buildStorage(const std::string& storage) override {
computer_.setStorage(storage);
}
Computer getResult() override {
return computer_;
}
private:
Computer computer_;
};
// Director
class ComputerAssembler {
public:
Computer assembleComputer(ComputerBuilder& builder) {
builder.buildCPU("Intel i7");
builder.buildMemory("16GB");
builder.buildStorage("512GB SSD");
return builder.getResult();
}
Computer assembleFastComputer(ComputerBuilder& builder) {
builder.buildCPU("Intel i5");
builder.buildMemory("8GB");
builder.buildStorage("256GB SSD");
return builder.getResult();
}
};
int main() {
DesktopComputerBuilder desktopBuilder;
ComputerAssembler assembler;
Computer desktop = assembler.assembleComputer(desktopBuilder);
std::cout << "Desktop Computer Configuration:" << std::endl;
desktop.display();
Computer fastDesktop = assembler.assembleFastComputer(desktopBuilder);
std::cout << "\nFast Desktop Computer Configuration:" << std::endl;
fastDesktop.display();
return 0;
}