forked from gennad/Design-Patterns-in-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
55 lines (41 loc) · 1.13 KB
/
proxy.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
# -*- coding: utf-8 -*-
class IMath:
"""Interface for proxy and real subject."""
def add(self, x, y):
raise NotImplementedError()
def sub(self, x, y):
raise NotImplementedError()
def mul(self, x, y):
raise NotImplementedError()
def div(self, x, y):
raise NotImplementedError()
class Math(IMath):
"""Real subject."""
def add(self, x, y):
return x + y
def sub(self, x, y):
return x - y
def mul(self, x, y):
return x * y
def div(self, x, y):
return x / y
class Proxy(IMath):
"""Proxy."""
def __init__(self):
self.math = Math()
def add(self, x, y):
return self.math.add(x, y)
def sub(self, x, y):
return self.math.sub(x, y)
def mul(self, x, y):
return self.math.mul(x, y)
def div(self, x, y):
if y == 0:
return float('inf') # Вернуть positive infinity
return self.math.div(x, y)
p = Proxy()
x, y = 4, 2
print '4 + 2 = ' + str(p.add(x, y))
print '4 - 2 = ' + str(p.sub(x, y))
print '4 * 2 = ' + str(p.mul(x, y))
print '4 / 2 = ' + str(p.div(x, y))