Created
May 29, 2019 15:33
-
-
Save SebDeclercq/8abd056b678fea565a975a4589cbbc18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from abc import ABC, abstractmethod | |
from dataclasses import dataclass | |
class Writer(ABC): | |
@abstractmethod | |
def write(self, text: str) -> None: | |
pass | |
class XML(Writer): | |
def write(self, text: str) -> None: | |
print('YEAH du XML !!') | |
print(text) | |
class CSV(Writer): | |
def write(self, text: str) -> None: | |
print('AAAH du CSV !!') | |
print(text) | |
@dataclass | |
class Config: | |
output_fmt: str = 'xml' | |
class Factory: | |
@classmethod | |
def init(cls, output_fmt: str) -> Writer: | |
if output_fmt == 'xml': | |
output: Writer = XML() | |
elif output_fmt == 'csv': | |
output: Writer = CSV() | |
return output | |
class App: | |
def __init__(self, config: Config = Config()) -> None: | |
self.output: Writer = Factory.init(config.output_fmt) | |
def hello(self) -> None: | |
self.output.write('hello world') | |
class T(Writer): | |
pass | |
def main() -> None: | |
app: App = App() | |
app.hello() | |
app = App(Config('csv')) | |
app.hello() | |
t: T = T() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment