Skip to content

Instantly share code, notes, and snippets.

@m0sth8
Last active April 7, 2016 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0sth8/93a3322a95731b9998b2511496d62613 to your computer and use it in GitHub Desktop.
Save m0sth8/93a3322a95731b9998b2511496d62613 to your computer and use it in GitHub Desktop.
# -*- 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