Last active
April 7, 2016 16:45
-
-
Save m0sth8/93a3322a95731b9998b2511496d62613 to your computer and use it in GitHub Desktop.
This file contains 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
# -*- coding: utf-8 -*- | |
class IMath: | |
"""Interface for proxy and real object""" | |
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): | |
"""Реальный субъект""" | |
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 = None | |
def add(self, x, y): | |
return x + y | |
def sub(self, x, y): | |
return x - y | |
def mul(self, x, y): | |
if not self.math: | |
self.math = Math() | |
return self.math.mul(x, y) | |
def div(self, x, y): | |
if y == 0: | |
return float('inf') | |
if not self.math: | |
self.math = Math() | |
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment