-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path3_builder_inheritance.py
47 lines (35 loc) · 1.1 KB
/
3_builder_inheritance.py
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
class Person:
def __init__(self):
self.name = None
self.position = None
self.date_of_birth = None
def __str__(self):
return f'{self.name} born on {self.date_of_birth} works as a {self.position}'
@staticmethod
def new():
return PersonBuilder()
class PersonBuilder:
def __init__(self):
self.person = Person()
def build(self):
return self.person
class PersonInfoBuilder(PersonBuilder):
def called(self, name):
self.person.name = name
return self
class PersonJobBuilder(PersonInfoBuilder):
def works_as_a(self, position):
self.person.position = position
return self
class PersonBirthDateBuilder(PersonJobBuilder):
def born(self, date_of_birth):
self.person.date_of_birth = date_of_birth
return self
if __name__ == '__main__':
pb = PersonBirthDateBuilder()
me = pb\
.called('Dmitri')\
.works_as_a('quant')\
.born('1/1/1980')\
.build() # this does NOT work in C#/C++/Java/...
print(me)