Proxy 模式为另一个对象提供一个占位符或代理来控制对它的访问。代理具有与真实对象相同的接口,但添加了行为——如延迟加载、访问控制、缓存或日志记录——然后委托给它。
Proxy 模式的作用
PROXY → an object that STANDS IN for another, controlling access to it:
→ same interface as the real object (clients use it transparently)
→ adds behavior BEFORE/AROUND delegating to the real object
→ controls WHEN and HOW the real object is accessed
→ "a stand-in that adds control/behavior"
代理的类型
VIRTUAL proxy → LAZY initialization → create/load the real object only when needed
(e.g. defer loading a heavy resource until first use)
PROTECTION proxy → ACCESS CONTROL → check permissions before allowing access
CACHING proxy → cache results → return cached data instead of re-fetching/recomputing
REMOTE proxy → represent an object in another location (network/remote calls hidden)
LOGGING/smart proxy → add logging, reference counting, etc. around access
