8000 finish Builder Design Pattern · jacobklo/jalgorithmCPP@70003ac · GitHub
[go: up one dir, main page]

Skip to content

Commit 70003ac

Browse files
committed
finish Builder Design Pattern
1 parent 58e5539 commit 70003ac

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ add_executable(jalgorithmCPP
6565
Implementation/Array/SpiralMatrix.h
6666
DesignPattern/Creational/AbstractFactory.h
6767
DesignPattern/Structural/Adapter.h
68-
)
68+
DesignPattern/Behavioral/Iterator.h DesignPattern/Creational/Builder.h)
6969

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Check out CustomDataStructure/Deque.h
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// Created by Jacob Lo on Feb 19, 2019
3+
//
4+
5+
#pragma once
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
namespace Builder {
11+
class Car {
12+
Car(string c, string e) : color(c), engine(e){}
13+
14+
public:
15+
const string color, engine;
16+
17+
struct CarBuilder {
18+
string color = "Red", engine = "Boxer";
19+
20+
public:
21+
CarBuilder& setColor(string c) {
22+
this->color = c;
23+
return *this;
24+
}
25+
CarBuilder& setEngine(string e) {
26+
this->engine = e;
27+
return *this;
28+
}
29+
30+
Car build() {
31+
return Car(color, engine);
32+
}
33+
};
34+
};
35+
36+
ostream& operator<< ( ostream& out, const Car& c) {
37+
out << "This is a " << c.color << " color car with " << c.engine << " engine." << endl;
38+
return out;
39+
}
40+
41+
void test() {
42+
Car c1 = Car::CarBuilder().setColor("Blue").setEngine("Inline").build();
43+
Car c2 = Car::CarBuilder().setColor("Black").setEngine("V6").build();
44+
45+
Car::CarBuilder builder;
46+
Car c3 = builder.build();
47+
48+
builder.setColor("Green");
49+
Car c4 = builder.build();
50+
51+
builder.setEngine("V8");
52+
Car c5 = builder.build();
53+
54+
cout << c1 << c2 << c3 << c4 << c5;
55+
}
56+
57+
}

src/main.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
using namespace std;
2222

23-
24-
2523
int main() {
2624

2725
}

0 commit comments

Comments
 (0)
0