-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbuilder_inheritance.py
More file actions
47 lines (35 loc) · 1012 Bytes
/
builder_inheritance.py
File metadata and controls
47 lines (35 loc) · 1012 Bytes
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, name):
self.name = name
self.position = None
self.date_of_birth = None
def __str__(self):
return f'{self.name} born on {self.date_of_birth} ' + \
f'works as {self.position}'
@staticmethod
def new():
return PersonBuilder()
class PersonBuilder:
def __init__(self):
self.person = Person(None)
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
PB = PersonBirthDateBuilder()
ME = PB\
.called('Dmitri')\
.works_as_a('Quant')\
.born('1/1/1980')\
.build()
print(ME)