Software design - Lecture 38. The main topics covered in this chapter include: proxy design pattern or surrogate design pattern; issues in accessing object services; solution and motivation for proxy; intend of proxy design pattern; proxy pattern defined;...
Trang 1Lecture : 38
Trang 2OR Surrogate Design Pattern
Trang 5In contrast, sometimes a client object may not be able to
access a service provider object (also referred to as a target object) by normal means may be due to:
The location of the target object – Remote Object
Uninstantiated Object till it is required to be rendered.
Special Behavior: Client object need special permission for
access
Trang 6Proxy
In such cases, instead of having client objects to deal with the special requirements for accessing the target object, the Proxy pattern suggests using a separate object
referred to as a proxy to provide a means for different
client objects to access the target object in a normal,
straightforward manner
Trang 7 The intent of this pattern is to provide a placeholder for an object to control references to it.
Trang 8“ Proxy design pattern provides a surrogate or placeholder for another object to control access to it “
Trang 10Client objects are no longer needed to deal with the special requirements for accessing the services of the target object
Trang 11A client can call the Proxy object through its interface and the Proxy object in turn forwards those calls to the target object
Client objects need not even know that they are dealing with Proxy for the original object
Trang 12The Proxy object hides the fact that a client object is dealing with an object that is either remote, unknown whether instantiated or not, or needs special authentication
In other words, a Proxy object serves as a transparent
bridge between the client and an inaccessible remote object or an object whose instantiation may have been deferred
Trang 13 Virtual Proxies: delaying the creation and initialization of expensive objects until needed, where the objects are created on demand (For example creating the RealSubject object only when the doSomething method is invoked)
Remote Proxies: providing a local representation for
an object that is in a different address space. A common example is Java RMI stub objects. The stub object acts as
a proxy where invoking methods on the stub would cause the stub to communicate and invoke methods on a remote object (called skeleton) found on a different machine
Trang 14 Protection Proxies: where a proxy controls
access to RealSubject methods, by giving access
to some objects while denying access to others.
Smart References: providing a sophisticated
access to certain objects such as tracking the number of references to an object and denying access if a certain number is reached, as well as loading an object from database into memory on demand.
•
Trang 15Class Diagram
Trang 17 A client obtains a reference to a Proxy, the client then handles the proxy in the same way it handles RealSubject and thus invoking the method doSomething().
At that point the proxy can do different things prior to invoking RealSubjects doSomething() method. The client might create a RealSubject object at that point, perform initialization, check permissions of the client to invoke the method, and then invoke the method on the object. The client can also do additional tasks after invoking the doSomething() method, such as incrementing the number of references to the object.
Trang 18of the remote object
Trang 19implementing the methods declared in the associated
remote interface.
RMI Registry — RMI registry provides the storage area
for holding different remote objects. It is directory of
remote objects with reference name which client can access
Trang 20 Once a remote object is compiled successfully, RMIC, the Java RMI stub compiler can be used to
generate stub and skeleton class files for the
remote object. Stub and skeleton classes are
generated from the compiled remote object class. These stub and skeleton classes make it possible for a client object to access the remote
object in a seamless manner.
Trang 22and is responsible for forwarding method invocations on
the remote object to the server where the actual remote object implementation resides. Whenever a client references the remote object, the reference is, in fact, made to a local stub. That means, when
a client makes a method call on the remote object, it is first received by the local stub instance. The stub forwards this call to the remote server. On the server the RMIC generated skeleton of the remote object receives this call.
As shown in diagram next
Trang 24 Consider an image viewer program that lists and displays high resolution photos. The program has
to show a list of all photos however it does not need to display the actual photo until the user selects an image item from a list.
Trang 25Class Diagram
Trang 27Java Code
Trang 29private String imageFilePath;
/** * Reference to RealSubject */
private Image proxifiedImage;
public ImageProxy(String imageFilePath) {
this.imageFilePath= imageFilePath;
}
Trang 32{ public static void main(String[] args)
{
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg"); Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg"); Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");
// assume that the user clicks on Image one item in a list // this would cause the program to call showImage() for that image only // note that in this case only image one was loaded into memory
highResolutionImage1.showImage();
Trang 33Image highResolutionImageNoProxy1 = new HighResolutionImage("veryHighResPhoto1.jpeg"); Image highResolutionImageNoProxy2 = new HighResolutionImage("veryHighResPhoto2.jpeg"); Image highResolutionImageBoProxy3 = new HighResolutionImage("veryHighResPhoto3.jpeg"); // assume that the user selects image two item from images list
highResolutionImageNoProxy2.showImage();
// note that in this case all images have been loaded into
memory // and not all have been actually displayed // this is a waste of memory resources } }
Trang 35 A Façade object represents a subsystem of objects.
The client object does have the ability to access the subsystem objects directly, if needed.
A Façade object provides a simplified higher level interface to a subsystem of components.