forked from gil9red/SimplePyScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
51 lines (32 loc) · 1.28 KB
/
Copy pathexample.py
File metadata and controls
51 lines (32 loc) · 1.28 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
# SOURCE: Design Patterns: Strategy — Стратегия
# SOURCE: https://ru.wikipedia.org/wiki/Стратегия_(шаблон_проектирования)
# SOURCE: https://javarush.ru/groups/posts/584-patternih-proektirovanija
from abc import ABC, abstractmethod
class Strategy(ABC):
@abstractmethod
def download(self, file: str):
pass
class DownloadWindowsStrategy(Strategy):
def download(self, file: str):
print("Windows download: " + file)
class DownloadLinuxStrategy(Strategy):
def download(self, file: str):
print("Linux download: " + file)
class Context:
def __init__(self, strategy: Strategy):
self._strategy = strategy
def set_strategy(self, strategy: Strategy):
self._strategy = strategy
def download(self, file: str):
self._strategy.download(file)
if __name__ == '__main__':
context = Context(DownloadWindowsStrategy())
context.download("file.txt") # Windows download: file.txt
print()
context = Context(DownloadLinuxStrategy())
context.download("file.txt") # Linux download: file.txt
context.set_strategy(DownloadWindowsStrategy())
context.download("file.txt") # Windows download: file.txt