-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuilder_inheritance.py
More file actions
62 lines (39 loc) · 1.69 KB
/
builder_inheritance.py
File metadata and controls
62 lines (39 loc) · 1.69 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
"""Builder pattern example: using inheritance to avoid violating OCP.
Whenever you need to add more information, you inherit from a builder that you've
already got.
"""
from typing import Optional, TypeVar
class Person:
"""Person class recording the person's name, job position, and date of birth."""
def __init__(self) -> None:
self.name: Optional[str] = None
self.position: Optional[str] = None
self.date_of_birth: Optional[str] = None
T = TypeVar("T", bound="PersonBuilder")
class PersonBuilder:
"""Root builder to be inherited to build various aspects of the `Person` object."""
def __init__(self) -> None:
self.person = Person()
def build(self) -> Person:
"""Build the `Person` object."""
return self.person
# You can have any number of builders through inheritance to initialise various aspects
# of Person.
class PersonInfoBuilder(PersonBuilder):
"""Build a `Person` along with the person's personal information."""
def called(self: T, name: str) -> T:
"""Set the person's name."""
self.person.name = name
return self
class PersonJobBuilder(PersonInfoBuilder):
"""Build a `Person` along with the person's personal and job information."""
def works_as_a(self: T, position: str) -> T:
"""Set the person's job position."""
self.person.position = position
return self
class PersonBirthDateBuilder(PersonJobBuilder):
"""Build a `Person` along with the person's personal and job information, and birth date."""
def born(self: T, date_of_birth: str) -> T:
"""Set the person's date of birth."""
self.person.date_of_birth = date_of_birth
return self