Message Forwarding Semi-automatic proxy delegation.

21
Message Forwarding Semi-automatic proxy delegation

Transcript of Message Forwarding Semi-automatic proxy delegation.

Page 1: Message Forwarding Semi-automatic proxy delegation.

Message Forwarding

Semi-automatic proxy delegation

Page 2: Message Forwarding Semi-automatic proxy delegation.

What is a proxy?

• An element that represents another element

• Example:– Proxy vote – slip that is sent to a central

authority in lieu of appearing and voting in person. The proxy vote represents the vote of the individual.

– proxy delegate – simpler interface to a complex system

Page 3: Message Forwarding Semi-automatic proxy delegation.

Bridge Pattern

• client with an interface

• server that implements the interface

• a means of communicating between the client and the server.

• The Goal is to have transparent distributed applications. Key for using RMI.

Page 4: Message Forwarding Semi-automatic proxy delegation.

What is message forwarding?

• A message is passed from one place to another.

• In OOP Message Forwarding means:– Taking a method invocation and forwarding it

to another instance.

Page 5: Message Forwarding Semi-automatic proxy delegation.

Example

class Mammal {

public boolean hasHair() {

return true;

}

}

Page 6: Message Forwarding Semi-automatic proxy delegation.

Movable class

class Movable {

int x, int y;

public void move(int _x, int _y) {

x = _x;

y = _y;

}

}

Page 7: Message Forwarding Semi-automatic proxy delegation.

MovableMammal

class MovableMammal {

Movable m; Mammal mm;

MovableMammal(Movable _m, Mammal _mm) {

m = _m;

mm = _mm;

}

.....

Page 8: Message Forwarding Semi-automatic proxy delegation.

MovableMammal...

public void move(int x, int y) {

m.move(x, y);

}

public boolean hasHair() {

return mm.hasHair();

}

}

Page 9: Message Forwarding Semi-automatic proxy delegation.

UML

• the proxy uses the implementation of another class

uses

delegator delegatee

e.g., MovableMammal

Page 10: Message Forwarding Semi-automatic proxy delegation.

What is a static proxy degation?

• Have method invocations that are checked at compile time.

• Adding methods to the delegatees does NOT mean that methods will appear in the proxy.

Page 11: Message Forwarding Semi-automatic proxy delegation.

What is dynamic delegation?

• New methods in the delegatee automatically appear available in the proxy at run-time.

• New feature for JDK 1.3

• Not type safe. Exceptions can be thrown.

• For example: using reflection to invoke a string on an instance.

Page 12: Message Forwarding Semi-automatic proxy delegation.

Semi-automatic Static Delegation

• Compile-time checks for type safety.

• Delgatee changes are discovered using introspection and cause a resynthesis of the proxy code.

• process of Java source code synthesis that occurs at a pre-processing run-time.

Page 13: Message Forwarding Semi-automatic proxy delegation.

A new Time

• design time (input instance to the synthesizer)

• generate time (output proxy code to a file)• compile time (compile the new proxy)• Linking time (dynamically link to the proxy

and classes it uses)• Run time (where invoke method in the

proxy)

Page 14: Message Forwarding Semi-automatic proxy delegation.

Why do semi-automatic static proxy delegation?

• Better interface to a complex system

• Better Code Reuse

• Easier than manual message forwarding

• Type safe

• Easier maintenance

• Alternative to inheritance

Page 15: Message Forwarding Semi-automatic proxy delegation.

What is wrong with using Multiple Inheritance?

• No MI in Java!

• Less reliable– duplicate methods with the same signature then

you must disambiguate– shadowing is decided at compile-time using

some technique– topological sorting is an automatic means of

disambiguating.

Page 16: Message Forwarding Semi-automatic proxy delegation.

Example of MI Confusion

class TrigRad {

public double cos(double d) {

return Math.cos(d);//in radians

}

}

Page 17: Message Forwarding Semi-automatic proxy delegation.

TrigDeg

class TrigDeg {

public double cos(double d) {

return Math.cos(d * Math.PI/180.0);

}

}

Page 18: Message Forwarding Semi-automatic proxy delegation.

MI and Trig

class Confusion extends TrigRad, TrigDeg {

double d = cos(90);

}// this is not allowed in Java but is

// in C++ or Smalltalk. Or in ZetaLisp

// Simula

Ambiguity can exist for topological sorting in MI.

Page 19: Message Forwarding Semi-automatic proxy delegation.

What is Diamond Inheritance?

• base class is sub-classed by two other classes that are in turn sub-classed by a 4th class.

Stream

IS OS

IOS

Page 20: Message Forwarding Semi-automatic proxy delegation.

What is wrong with Diamond Inheritance?

• Has all the problems of multiple inheritance.

• More complex for linking stage.

• Overhead for each instance grows.

• Most of the overhead is in malloc or NEW

• memory is taken from the HEAP.

• Fragile base class (constant recompilation)

Page 21: Message Forwarding Semi-automatic proxy delegation.

What is good about Diamond Inheritance?

• Automatic addition of features to a subclass.

• Single large class.

• Toolkits are easier to create