Design Pattern Examples
Overview of object-oriented design patterns
proxy_class.py
Go to the documentation of this file.
8
9from .proxy_interface import IWorkByProxy
10from .proxy_class_real import Real_Classes_Container
11
12
20
21
22 def __init__(self) -> None:
23 self._realClassInstance = None # type: IWorkByProxy
24
25
28
29
30
39 def _GetRealClass(self) -> IWorkByProxy:
40 if not self._realClassInstance:
41 print(" --> Creating instance of real class...")
42 self._realClassInstance = Real_Classes_Container.CreateReal()
43 return self._realClassInstance
44
45
46 #-------------------------------------------------
47 # Implementation of the IWorkByProxy interface
48 #-------------------------------------------------
49
50
62 def DoWork(self, someArgument : str) -> str:
63 print(" --> proxy class DoWork() in")
64 realClass = self._GetRealClass()
65 print(" --> Forwarding DoWork() call to real class...")
66 return realClass.DoWork(someArgument)
67
68
69#========================================================================
70#========================================================================
71
72
73
81
82
88 def CreateProxy() -> IWorkByProxy:
89 return Proxy_Class()
90
91
92
95__all__ = ["Proxy_Classes_Container"]
The proxy class that implements the IWorkByProxy interface.
Definition: proxy_class.py:19
_realClassInstance
The one and only instance of the real class associated with this proxy class instance.
Definition: proxy_class.py:23
IWorkByProxy _GetRealClass(self)
Helper method to retrieve the one and only instance of the real class.
Definition: proxy_class.py:39
str DoWork(self, str someArgument)
Do some work on a string.
Definition: proxy_class.py:62
IWorkByProxy CreateProxy()
Retrieve a new instance of the proxy class.
Definition: proxy_class.py:88
Represents what can be done on the proxy object.