@@ -0,0 +1,1772 @@
+%22%22%22Proxy pattern%0A%0AProxy is a structural design pattern. A proxy is a surrogate object which can%0Acommunicate with the real object (aka implementation). Whenever a method in the%0Asurrogate is called, the surrogate simply calls the corresponding method in%0Athe implementation. The real object is encapsulated in the surrogate object when%0Athe latter is instantiated. It's NOT mandatory that the real object class and%0Athe surrogate object class share the same common interface.%0A%22%22%22%0Afrom abc import ABC, abstractmethod%0A%0A%0Aclass CommonInterface(ABC):%0A    %22%22%22Common interface for Implementation (real obj) and Proxy (surrogate).%22%22%22%0A%0A    @abstractmethod%0A    def load(self):%0A        pass%0A%0A    @abstractmethod%0A    def do_stuff(self):%0A        pass%0A%0A%0Aclass Implementation(CommonInterface):%0A%0A    def __init__(self, filename):%0A        self.filename = filename%0A%0A    def load(self):%0A        print('load %7B%7D'.format(self.filename))%0A%0A    def do_stuff(self):%0A        print('do stuff on %7B%7D'.format(self.filename))%0A%0A%0Aclass Proxy(CommonInterface):%0A%0A    def __init__(self, implementation):%0A        self.__implementation = implementation  # the real object%0A        self.__cached = False%0A%0A    def load(self):%0A        self.__implementation.load()%0A        self.__cached = True%0A%0A    def do_stuff(self):%0A        if not self.__cached:%0A            self.load()%0A        self.__implementation.do_stuff()%0A%0A%0Adef main():%0A    p1 = Proxy(Implementation('RealObject1'))%0A    p2 = Proxy(Implementation('RealObject2'))%0A%0A    p1.do_stuff()  # loading necessary%0A    p1.do_stuff()  # loading unnecessary (use cached object)%0A    p2.do_stuff()  # loading necessary%0A    p2.do_stuff()  # loading unnecessary (use cached object)%0A    p1.do_stuff()  # loading unnecessary (use cached object)%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add Proxy pattern