-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactory.py
60 lines (44 loc) · 1.4 KB
/
factory.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
48
49
50
51
52
53
54
55
56
57
58
59
60
# --------------------------------------------------------
# Licensed under the terms of the BSD 3-Clause License
# (see LICENSE for details).
# Copyright © 2018-2024, A.A Suvorov
# All rights reserved.
# --------------------------------------------------------
# https://github.com/smartlegionlab/
# --------------------------------------------------------
"""Factory method"""
from abc import ABC, abstractmethod
class Document(ABC):
@abstractmethod
def show(self):
"""Show document"""
class PDFDocument(Document):
def show(self):
print('PDF document format.')
class ODFDocument(Document):
def show(self):
print('ODF document format.')
class NoneDocument(Document):
def show(self):
print('None type document format')
class ApplicationBase(ABC):
@abstractmethod
def create_doc(self, type_):
"""Create document"""
class Application(ApplicationBase):
def create_doc(self, type_):
if type_ == 'pdf':
return PDFDocument()
elif type_ == 'odf':
return ODFDocument()
else:
return NoneDocument()
def main():
# Creating application
app = Application()
# Creating docs
app.create_doc('pdf').show() # PDF document format.
app.create_doc('odf').show() # ODF document format.
app.create_doc('bad').show() # None type document format
if __name__ == '__main__':
main()