14
Proxy Pattern

Proxy Pattern

Embed Size (px)

DESCRIPTION

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 different address space) The proxy looks like the remote object, but isn’t - PowerPoint PPT Presentation

Citation preview

Page 1: Proxy Pattern

Proxy PatternProxy Pattern

Page 2: Proxy Pattern

What’s a Proxy?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 different address space)

• The proxy looks like the remote object, but isn’t

• The proxy takes method calls and handles all the details of network communication to the remote object

• A remote proxy acts as a local representative of a remote object

• Remote Object: instantiated in a different JVM heap (a different address space)

• The proxy looks like the remote object, but isn’t

• The proxy takes method calls and handles all the details of network communication to the remote object

Page 3: Proxy Pattern

What’s a Proxy?What’s a Proxy?

ClientCode

Proxy

Local Heap Remote Heap

RemoteObject

Page 4: Proxy Pattern

RMIRMI• Luckily, we don’t have to write the code to

handle network communications• RMI: Remote Method Invocation

• Java provides packages that we can easily use to make our current code ready for network communication with very few changes

• The book provides a very detailed guide to this process

• Luckily, we don’t have to write the code to handle network communications

• RMI: Remote Method Invocation

• Java provides packages that we can easily use to make our current code ready for network communication with very few changes

• The book provides a very detailed guide to this process

Page 5: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

Proxy, pretends to be the actual service.Packs requests and handles network communications

Unpacks requests from client helper; calls requests on the Service Object

Page 6: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

doBigJob()

Page 7: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

doBigJob()

Client helper packages up the info about the method call (Serializable) & ships it over the network

“client wants to call a method”

Page 8: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

Service helper packs the result and handles network communications

result

Page 9: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

Client helper unpacks the result and passes it back to the Client Object

packaged result

Page 10: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

The Client Object doesn’t know it isn’t talking to the Service Object in its own heap!

result

Page 11: Proxy Pattern

RMIRMI

ClientObject

Client helper

Local Heap Remote Heap

ServiceObjectService

helper

Newer versions of Java don’t require an explicit skeleton object, but something is still taking care of the skeleton behavior.

RMI STUBRMI

SKELETON

Page 12: Proxy Pattern

Making a Remote ServiceMaking a Remote Service1. Make a remote Interface

Defines the methods that can be called remotelyStub and Service Object will implement it

2. Make a Remote ImplementationThis is the class that does the “real work”

3. Rmic to generate stubs & skeletons (Server)Rmic comes with the Java SDKImplementation_stub.classImplementation_Skel.class

4. Start the registry (Server): rmiregistry 5. Start the remote service (server)

Needs to be in a separate thread from the rmiregistry

1. Make a remote InterfaceDefines the methods that can be called remotelyStub and Service Object will implement it

2. Make a Remote ImplementationThis is the class that does the “real work”

3. Rmic to generate stubs & skeletons (Server)Rmic comes with the Java SDKImplementation_stub.classImplementation_Skel.class

4. Start the registry (Server): rmiregistry 5. Start the remote service (server)

Needs to be in a separate thread from the rmiregistry

Page 13: Proxy Pattern

Back to the PatternBack to the PatternDEFINITION:

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

DEFINITION:

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.

Page 14: Proxy Pattern

Class diagramClass diagram

<<interface>>Subject

request()

RealSubject

request()

Proxy

request()

subject