Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.

Similar presentations


Presentation on theme: "The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1."— Presentation transcript:

1 The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1

2 Motivation There are many situations in which a client does not or can not reference an object directly, but the client still wants to interact with the object. A proxy object can act as an intermediary between the client and the target object. Consider, for example, the idea of distributed objects, where objects in an application are distributed across multiple computer systems. How does a client on one computer access an object that resides on a different computer? ©SoftMoore ConsultingSlide 2

3 Motivation (continued) One common approach is to have a local proxy running on the client’s computer that “looks like” the target object on the remote computer. The local proxy forwards requests across the network to the target object. From a clients perspective, there is (essentially) no difference between communicating with a proxy versus communicating with the target object. The proxy hides the complexities of sending requests across a network. ©SoftMoore ConsultingSlide 3

4 Proxy Pattern Intent: Provide a surrogate or placeholder for another object to control access to it. Also Known As: Surrogate Applicability: Proxy is applicable whenever there is a need for a more versatile or sophisticated reference to an object than a simple pointer. Examples include –A remote proxy provides a local representative for an object in a different address space. –A virtual proxy creates expensive objects on demand. –A protection proxy controls access to the original object. –A smart pointer is a replacement for a bare pointer that performs additional actions when the object is accessed (e.g., reference counting, mutual exclusion, etc.) Slide 4©SoftMoore Consulting

5 Proxy Pattern (continued) ©SoftMoore ConsultingSlide 5 Client Subject request()... RealSubject request()... Structure Proxy request()... realSubject.request() realSubject : Client: Proxy: RealSubject Object Diagram

6 Proxy Pattern (continued) Participants Proxy –maintains a reference that lets the proxy access the real subject. –provides an interface identical to that of Subject so that a proxy can be substituted for the real subject. –controls access to the real subject and may be responsible for creating/deleting it. –other responsibilities depending on the kind of proxy. remote proxies encode a request and its parameters for sending to the real subject in a different address space (e.g., over a network) virtual proxies cache information about the real subject so that they can postpone accessing it. protection proxies check that the caller has the required access permissions. Slide 6©SoftMoore Consulting

7 Proxy Pattern (continued) Participants (continued) Subject –defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected. RealSubject –defines the real object that the proxy represents. Slide 7©SoftMoore Consulting

8 Proxy Pattern (continued) Collaborations –Proxy forwards requests to RealSubject when appropriate, depending on the kind of proxy. Consequences: The Proxy pattern introduces a level of indirection when accessing an object. The additional indirection has many uses, depending on the kind of proxy. –A remote proxy can hide the fact that the object resides in a different address space. –A virtual proxy can perform optimizations such as creating and object on demand. –Both protection proxies and smart pointers perform additional housekeeping tasks when an object is accessed. ©SoftMoore ConsultingSlide 8

9 Proxy Pattern (continued) Implementation Implementing a proxy is often straightforward (but not necessarily simple) since the proxy essentially forwards requests to the target object, possibly with some additional actions before or after forwarding the request. The Proxy pattern can exploit language-specific features in some cases; e.g., –overloading the dereferencing operators -> and * in C++ –using doesNotUnderstand in Smalltalk to catch and forward requests to the target object. ©SoftMoore ConsultingSlide 9

10 Proxy Pattern in Java: RMI Remote Method Invocation (RMI) is a Java-specific technology for distributed objects. RMI uses one interface and two implementations The interface defines exported behavior. Two implementations are created: –remote: implements actual behavior –local: proxy links to remote object A local program, using the local proxy, is tricked into thinking it is directly connected to a remote object ©SoftMoore ConsultingSlide 10

11 ©SoftMoore ConsultingSlide 11 Proxy Pattern in Java: RMI (continued) Transport Layer Remote Reference Layer Stubs and Skeletons Client ProgramServer Program RMI System RMI Architecture

12 Related Patterns Both Proxy and Adapter introduce a level of indirection to other objects, but an adapter provides a different interface to the object it adapts, whereas a proxy provides the same interface. Decorators have similar implementations as proxies, but they have different purposes. A decorator adds one or more responsibilities to an object, whereas a proxy controls access to it. A Proxy is essentially a Bridge with only one implementation (degenerate bridge), where the abstraction and implementation share the same interface. ©SoftMoore ConsultingSlide 12

13 References Proxy pattern (Wikipedia) https://en.wikipedia.org/wiki/Proxy_pattern Proxy Pattern (Object-Oriented Design) http://www.oodesign.com/proxy-pattern.html Proxy Design Pattern (SourceMaking) http://sourcemaking.com/design_patterns/proxy Take control with the Proxy design pattern (David Geary – JavaWorld) http://www.javaworld.com/article/2074068/learn-java/take-control-with-the-proxy-design-pattern.html ©SoftMoore ConsultingSlide 13


Download ppt "The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1."

Similar presentations


Ads by Google