Proxy:
class Implementation(object):
def __init__(self):
pass
def f(self):
print "In implementation function F."
def g(self):
print "In implementation function G."
def h(self):
print "In implementation function H."
class Proxy(object):
def __init__(self):
self.__imp = Implementation()
def __getattr__(self, name):
return getattr(self.__imp, name)
def main():
proxy = Proxy()
proxy.f()
proxy.g()
proxy.h()
State:
class State(object):
def __init__(self, imp):
self.__imp__ = imp
def changeState(self, imp):
self.__imp__ = imp
def __getattr__(self, name):
return getattr(self.__imp__, name)
class Implementation(object):
def __init__(self):
pass
def f(self):
print "In class Implementation function F."
def g(self):
print "In class Implementation function G."
def h(self):
print "In class Implementation function H."
class Implementation2(object):
def __init__(self):
pass
def f(self):
print "In class Implementation2 function F."
def g(self):
print "In class Implementation2 function G."
def h(self):
print "In class Implementation2 function H."
def main():
state = State(Implementation())
state.f()
state.g()
state.h()
state.changeState(Implementation2())
state.f()
state.g()
state.h()
if __name__ == '__main__':
main()