Created
January 29, 2019 07:27
-
-
Save rivmar/e5b5a0cb79180d6ab4925477988ca344 to your computer and use it in GitHub Desktop.
Draft of proxy pattern realisation
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
from __future__ import print_function | |
from collections import namedtuple | |
agx = namedtuple('art', 'label size') | |
a = agx('gold', 12) | |
b = agx('silver', 4) | |
class MyProxy(object): | |
def __new__(cls, agx_obj, *args, **kwargs): | |
client = PrintGold if agx_obj.label == 'gold' else PrintSilver | |
return object.__new__(client, agx_obj, *args, **kwargs) | |
def __init__(self, agx_obj, *args, **kwargs): | |
return self.output(agx_obj, *args, **kwargs) | |
def output(self, agx_obj, *args, **kwargs): | |
print('Main output', agx_obj.label) | |
class PrintGold(MyProxy): | |
def output(self, agx_obj, status, s=None, m=None): | |
print('Wow, it\'s gold, the size is', agx_obj.size) | |
print(s) | |
class PrintSilver(MyProxy): | |
def output(self, agx_obj, status, s=None, m=None): | |
print('Not bad, it\'s not a gold, but the size is', agx_obj.size) | |
print(status) | |
MyProxy(a, 'bajka', s=5) | |
MyProxy(b, 'manka', m=3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment