Software Design Lecture : 38.

Slides:



Advertisements
Similar presentations
Message Passing Vs Distributed Objects
Advertisements

Copyright © 2001 Qusay H. Mahmoud RMI – Remote Method Invocation Introduction What is RMI? RMI System Architecture How does RMI work? Distributed Garbage.
Remote Method Invocation in Java Bennie Lewis EEL 6897.
Advanced Programming Rabie A. Ramadan Lecture 4. A Simple Use of Java Remote Method Invocation (RMI) 2.
Remote Method Invocation
Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.
Sockets  Defined as an “endpoint for communication.”  Concatenation of IP address + port.  Used for server-client communication.  Server waits for.
EEC-681/781 Distributed Computing Systems Lecture 5 Wenbing Zhao Department of Electrical and Computer Engineering Cleveland State University
How Does Remote Method Invocation Work? –Systems that use RMI for communication typically are divided into two categories: clients and servers. A server.
Design Patterns Part IV (TIC++V2:C10) Yingcai Xiao 10/01/08.
Communication in Distributed Systems –Part 2
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Proxy Design Pattern (1) –A structural design pattern.
Client/Server Software Architectures Yonglei Tao.
Understanding the CORBA Model. What is CORBA?  The Common Object Request Broker Architecture (CORBA) allows distributed applications to interoperate.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
1 The Proxy Design Pattern Problem: Defer the cost of object creation and init. until actually used Applicability (possible contexts): – Virtual Proxy:
Team 5: The Infinite Loops Gloria Berumen Patricia Martinez Jose Roberto Salcido Michelle Soto Jose Luis Yanez Omar Zorrilla Design Pattern.
In the name of Allah The Proxy Pattern Elham moazzen.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 RMI.
RMI RMI is the java API that facilitate distributed computing by allowing remote method calls. A remote method call represents a method invocation between.
Java Remote Method Invocation RMI. Idea If objects communicate with each other on one JVM why not do the same on several JVM’s? If objects communicate.
RMI remote method invocation. Traditional network programming The client program sends data to the server in some intermediary format and the server has.
RMI Remote Method Invocation Distributed Object-based System and RPC Together 2-Jun-16.
 Remote Method Invocation  A true distributed computing application interface for Java, written to provide easy access to objects existing on remote.
The Proxy Pattern SE-2811 Dr. Mark L. Hornick 1. The Proxy Pattern has many variations, but in general: The Proxy Pattern uses an proxy object as a surrogate.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Proxy.
Proxy Design Pattern By:Diksha Agarwal.
Eric Tryon Brian Clark Christopher McKeowen. System Architecture The architecture can be broken down to three different basic layers Stub/skeleton layer.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Proxy Pattern. What’s a Proxy? A remote proxy acts as a local representative of a remote object Remote Object: instantiated in a different JVM heap (a.
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
UMBC Distributed Computing with Objects RMI/Corba CMSC 432 Shon Vick.
Java Distributed Object Model A remote object is one whose methods can be invoked from another JVM on a different host. It implements one or more remote.
1 Lecture 15 Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
Week 9, Day 1 Proxy SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Software Design and Architecture Muhammad Nasir Structural Design Patterns
Distributed Computing & Embedded Systems Chapter 4: Remote Method Invocation Dr. Umair Ali Khan.
RMI1 Remote Method Invocation Adapted from “Core Java 2” by Cay Horstmann.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns – Group 2 Iterator, Proxy, Adapter, Decorator.
Copyright © Jim Fawcett Spring 2017
Interfaces Unit 08.
Chapter 11 Object-Oriented Design
Broker in practice: Middleware
Dynamic Proxy Proxy: Addition to the Java 1.3 reflection package:
Remote Method Invocation
Chapter 2: System Structures
What is RMI? Remote Method Invocation
Whether you decide to use hidden frames or XMLHttp, there are several things you'll need to consider when building an Ajax application. Expanding the role.
Remote Method Invocation
How to be a Good Developer
Programming Models for Distributed Application
O.S Lecture 13 Virtual Memory.
Chapter 40 Remote Method Invocation
Remote method invocation (RMI)
Prof. Leonardo Mostarda University of Camerino
Chapter 46 Remote Method Invocation
Chapter 46 Remote Method Invocation
Software Design Lecture : 35.
CS 584 Lecture 18 Assignment Glenda assignment extended to the Java RMI Deadline No Java RMI Assignment Test Friday, Saturday, Monday.
Java Remote Method Invocation
15. Proxy SE2811 Software Component Design
Proxy Pattern Definition of “Proxy”: Authority or power to act for another Original Gang of Four pattern, much used Stands in for a “real object” (similar.
Software Design Lecture : 28.
Plug-In Architecture Pattern
Java Chapter 5 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Software Design Lecture : 38

Proxy Design Pattern OR Surrogate Design Pattern

Sample Code fragment //Client class Customer{ public void someMethod(){ //Create the Service Provider Instance FileUtil futilObj=new FileUtil(); //Access the Service futilObj.writeToFile(“Some Data”); }}

Implementation of Code As part of its implementation, the Customer class creates an instance of the FileUtil class and directly accesses its services. In other words, for a client object, the way of accessing a FileUtil object is fairly straightforward. From the implementation it seems to be the most commonly used way for a client object to access a service provider object

Issues in Accessing Object Services In 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

Solution and Motivation for Proxy 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.

Intend of Proxy Design Pattern The intent of this pattern is to provide a placeholder for an object to control references to it.

Proxy Pattern Defined “ Proxy design pattern provides a surrogate or placeholder for another object to control access to it “

Working of Proxy Design Pattern The Proxy object offers the same interface as the target object. The Proxy object interacts with the target object on behalf of a client object and takes care of the specific details of communicating with the target object.

Proxy Design Pattern Client objects are no longer needed to deal with the special requirements for accessing the services of the target object.

Working of Proxy Design Pattern A 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.

Proxy Design Pattern The 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

Type of Proxies 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

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.

Class Diagram

Flow of Application 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.

Remote Method Invocation -RMI Remote Interface — A remote object must implement a remote interface (one that extends java.rmi.Remote). A remote interface declares the methods in the remote object that can be accessed by its clients. In other words, the remote interface can be seen as the client’s view of the remote object

Remote Object — A remote object is responsible for implementing 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 Client — Client is an application object attempting to use the remote object. – Can search for a remote object using a name reference in the RMI Registry. Once the remote object reference is found, it can invoke methods on this object reference.

RMIC Java RMI Stub Compiler : 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.

Communication Mechanism In general, a client object cannot directly access a remote object by normal means. In order to make it possible for a client object to access the services of a remote object as if it is a local object, the RMIC-generated stub of the remote object class and the remote interface need to be copied to the client computer.

iii. The stub acts as a (Remote) proxy for the remote object and 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

Example of Proxy Design Pattern 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.

Class Diagram

Java Code

package proxy; /. Subject Interface package proxy; /** * Subject Interface */ public interface Image { public void showImage(); }

package proxy; /** * Proxy */ public class ImageProxy implements Image { private String imageFilePath; /** * Reference to RealSubject */ private Image proxifiedImage; public ImageProxy(String imageFilePath) this.imageFilePath= imageFilePath; }

public void showImage() { // create the Image Object only when the image is required to be shown proxifiedImage = new HighResolutionImage(imageFilePath); // now call showImage on realSubject proxifiedImage.showImage(); } }

package proxy; /. RealSubject package proxy; /** * RealSubject */ public class HighResolutionImage implements Image { public HighResolutionImage(String imageFilePath) loadImage(imageFilePath); } private void loadImage(String imageFilePath) { // load Image from disk into memory // this is heavy and costly operation @Override public void showImage() { // Actual Image rendering logic } }

public class ImageViewer { public static void main(String[] args) { package proxy; public class ImageViewer { 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();

// consider using the high resolution image object directly Image 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 } }

Comparison of Proxy with other Facade A Proxy object represents a single object. The client object cannot access the target object directly. A Proxy object provides access control to the single target object.

Facade 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.