-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathbuilder.cpp
More file actions
65 lines (56 loc) · 1.77 KB
/
builder.cpp
File metadata and controls
65 lines (56 loc) · 1.77 KB
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
// Builder
#include <vector>
class foo
{
public:
class builder;
foo(int prop1, bool prop2, bool prop3, std::vector<int> prop4)
: prop1{prop1}, prop2{prop2}, prop3{prop3}, prop4{prop4}
{ }
int prop1;
bool prop2;
bool prop3;
std::vector<int> prop4;
};
class foo::builder
{
public:
builder& set_prop1(int value) { prop1 = value; return *this; };
builder& set_prop2(bool value) { prop2 = value; return *this; };
builder& set_prop3(bool value) { prop3 = value; return *this; };
builder& set_prop4(std::vector<int> value) { prop4 = value; return *this; };
foo build() const
{
return foo{prop1, prop2, prop3, prop4};
}
private:
int prop1 = 0;
bool prop2 = false;
bool prop3 = false;
std::vector<int> prop4 = {};
};
int main()
{
foo f = foo::builder{}.set_prop1(5)
.set_prop3(true)
.build();
}
// Separate the complex construction of an object from its
// representation.
//
// The `foo` class, on [5-18], has a complex construction process
// during which any subset of its properties might be set. This
// process is captured by the `foo::builder` class, on [20-38].
// This builder class provides an interface for
// constructing `foo` objects, allowing various combinations of
// parameters to be provided. This avoids having to define a large
// collection of constructors for `foo`.
//
// The `foo::builder` class implements a set of
// chainable functions for setting the construction parameters
// ([23-26]) and a `build` member function for constructing the `foo`
// object with these parameters ([28-31]).
//
// On [42-44], we use `foo::builder` to construct a `foo` object,
// setting its `prop1` and `prop3` members and calling `build` to
// construct the object.