Created
July 9, 2013 15:04
-
-
Save AndrewDowning/5958103 to your computer and use it in GitHub Desktop.
Borg pattern demo
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
class Borg(object): | |
_state = {} | |
def __new__(cls, *p, **k): | |
self = object.__new__(cls, *p, **k) | |
self.__dict__ = cls._state | |
return self | |
class MyBaseClass(object): | |
def __init__(self, base_thing): | |
self.base_thing = base_thing | |
class MyBorgClass(Borg, MyBaseClass): | |
def __init__(self, thing=None): | |
if thing: | |
MyBaseClass.__init__(self, "Base_"+thing) | |
self.thing = thing | |
class Subordinate(object): | |
def __init__(self): | |
print("Subordinate accessing Base",MyBorgClass().base_thing) | |
first_instance = MyBorgClass("Original") | |
second_instance = MyBorgClass() | |
print (first_instance.thing, second_instance.thing) | |
sub = MyBorgClass.Subordinate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment