-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluent_builder.go
53 lines (44 loc) · 1.21 KB
/
fluent_builder.go
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
package creational
/*
Summary:
Fluent Builder helps in creating a complex object in steps using
method chaining technique.
Example:
Create an Employee object which has many user specified parameters.
With this approach we construct the object easily in one line (readability):
emp = builder.Called("Bezos").Company("Amazon").Address("USA").Build()
Benefit: Readability
When the Constructor is a a bad choice to construct an object?
When the constructor depends on huge number of user specified parameters,
using the fluent method chaining simplies the object creation in steps.
*/
type Employee struct {
Name string
Company string
Address string
}
type EmployeeBuilder struct {
name, address, company string
}
func NewEmployeeBuilder() *EmployeeBuilder {
return &EmployeeBuilder{}
}
func (c *EmployeeBuilder) WithName(name string) *EmployeeBuilder {
c.name = name
return c
}
func (c *EmployeeBuilder) WithCompany(company string) *EmployeeBuilder {
c.company = company
return c
}
func (c *EmployeeBuilder) WithAddress(address string) *EmployeeBuilder {
c.address = address
return c
}
func (c *EmployeeBuilder) Build() Employee {
return Employee{
Name: c.name,
Company: c.company,
Address: c.address,
}
}