-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_pattern.py
More file actions
69 lines (48 loc) · 1.73 KB
/
Copy pathbuilder_pattern.py
File metadata and controls
69 lines (48 loc) · 1.73 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
66
67
68
69
class NetworkService:
def __init__(self, url: str = "", auth: str = "", cache: int = 0):
self.components = {}
if url:
self.components["URL"] = url
if auth:
self.components["Authorization"] = auth
if cache:
self.components["Cache-Control"] = cache
def show(self):
print(self.components)
if __name__ == "__main__":
service1 = NetworkService(url="google.com")
service1.show()
service2 = NetworkService(url="youtube.com", auth="abc123", cache=60000)
service2.show()
# you have the option to create different versions of the same class as instance.
# Its not mandatory for all the objects to be added here
# class NetworkService:
# def __init__(self):
# self.components = {}
# def add(self, key: str, value: str):
# self.components[key] = value
# def show(self):
# print(self.components)
# class NetworkServiceBuilder:
# def __init__(self):
# self._service = NetworkService()
# def add_target_url(self, url: str):
# self._service.add("URL", url)
# def add_auth(self, auth: str):
# self._service.add("Authorization", auth)
# def add_caching(self, cache: int):
# self._service.add("Cache-Control", cache)
# def build(self) -> NetworkService:
# service = self._service
# self._service = NetworkService()
# return service
# if __name__ == "__main__":
# builder = NetworkServiceBuilder()
# builder.add_target_url("google.com")
# service1 = builder.build()
# service1.show()
# builder.add_target_url("youtube")
# builder.add_auth("abc123")
# builder.add_caching(60000)
# service2 = builder.build()
# service2.show()