-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencapsulation-composition.py
More file actions
49 lines (32 loc) · 971 Bytes
/
encapsulation-composition.py
File metadata and controls
49 lines (32 loc) · 971 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
48
49
import json
from abc import abstractmethod
import requests
import yaml
class BankAccount:
TOTAL_BANK_ACCOUNTS = 0
def __init__(self):
BankAccount.TOTAL_BANK_ACCOUNTS += 1
self.count_id = BankAccount.TOTAL_BANK_ACCOUNTS
# Encapsulation vs Composition -> The same think
# Encapsulation vs Inherence ->
class WriteFile:
def write_file(self, data):
data_to_write = self._format(data)
return data_to_write
# file.write(data_to_write)
@abstractmethod
def _format(data):
pass
class WriteFileJSON(WriteFile):
def _format(data):
return json.dumps(data)
class WriteFileYAML(WriteFile):
def _format(data):
return yaml.dumps(data)
class LoadAndSave:
writer: WriteFile
def __init__(self, writer: WriteFile = WriteFileYAML()):
self.writer = writer
def load_save(self):
data = requests.get("https://google.com")
self.writer.write_file(data)