Documente Academic
Documente Profesional
Documente Cultură
(TIC++V2:C10)
More Characteristics
reusable: reuse at the design level, generic, well-documented object-oriented: uses classes, objects, generalization, and polymorphism
Object Diagram
Components of a Proxy
Proxy:
maintains a reference to let proxy access the real subject provides an interface identical to Subjects so a proxy can be substituted for the real subject controls access to the real subject; may be responsible for creating and deleting it
More Participants
Subject:
defines the common interface for RealSubject and Proxy so a Proxy can be used anywhere a RealSubject is expected
RealSubject:
defines the real object that the proxy represents
Collaborations
Proxy forwards request to RealSubject when appropriate, depending on the kind of proxy
Sequence Diagram
Collaboration Diagram
Consequences
proxy pattern introduces a level of indirection when accessing an object
a remote proxy can hide the fact that an object resides in a different address space a virtual proxy can perform optimizations such as creating an object on demand protection proxies and smart references allow additional tasks when an object is referenced
Implementation
A proxy can exploit the following features: Java: use interface and implementation. C++: use virtual functions and overloading the member access operators. Smalltalk: use doesNotUnderstand, which supports automatic forwarding of requests Proxy doesnt have to know the type of the real object (upcasted to Object in Java)
Known Uses
Stubs in Java RMI. Proxy server in networking NEXTSTEP uses proxies as local representatives for objects that may be distributed Proxies in Smalltalk to access remote objects
Related Patterns
adapter: provides a different interface to the object it adapts; proxy provides the same interface as its subject decorator: adds one or more responsibilities to an object; proxy controls access to an object a protection proxy might be implemented exactly like a decorator