Message Forwarding Semi-automatic proxy delegation.
-
Upload
hugo-braswell -
Category
Documents
-
view
226 -
download
2
Transcript of Message Forwarding Semi-automatic proxy delegation.
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
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.
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.
Example
class Mammal {
public boolean hasHair() {
return true;
}
}
Movable class
class Movable {
int x, int y;
public void move(int _x, int _y) {
x = _x;
y = _y;
}
}
MovableMammal
class MovableMammal {
Movable m; Mammal mm;
MovableMammal(Movable _m, Mammal _mm) {
m = _m;
mm = _mm;
}
.....
MovableMammal...
public void move(int x, int y) {
m.move(x, y);
}
public boolean hasHair() {
return mm.hasHair();
}
}
UML
• the proxy uses the implementation of another class
uses
delegator delegatee
e.g., MovableMammal
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.
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.
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.
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)
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
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.
Example of MI Confusion
class TrigRad {
public double cos(double d) {
return Math.cos(d);//in radians
}
}
TrigDeg
class TrigDeg {
public double cos(double d) {
return Math.cos(d * Math.PI/180.0);
}
}
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.
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
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)
What is good about Diamond Inheritance?
• Automatic addition of features to a subclass.
• Single large class.
• Toolkits are easier to create