Can someone explain Letter/Envelope? Topic: Patterns
Jackie Noorden, May 7, 2001 [replies:12]
My C++ is too weak to understand any of the examples I can find for the Letter/Envelope idiom. If anyone can briefly supply me a Java based explanation I would be sooooo happy :)
Thanks,
Ms Jackie
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|



 |
Re: Can someone explain Letter/Envelope Topic: Patterns
Michael Wax, May 8, 2001 [replies:10]
I believe that it would be the bridge pattern. The rationale behind the letter/envelope idiom is to allow multiple implementations of an abstract datatype (either over time or in different application domains), but with no change in the client code. This is achieved in C++ by using an abstract base class from which are derived the both the interface (envelope) class, and the implementation (letter) classes. Calls to envelope methods are delegated to the corresponding letter methods.
An example: Suppose you were doing experiments which produced noisy data, and that you wanted to smooth this data to make long-range trends more apparent. However, you might not be sure which smoothing algorithm would work best, and further you would probably want to refine your chosen algorithm over time for greater efficiency. Using the bridge pattern:
public interface Smoothable {
public double[] smooth (double[] data);
}
public class Envelope {
private Smoothable impl;
public Envelope (Smoothable impl) {
this.impl=impl;
}
public double[] smooth(double[] data) {
return impl.smooth(data);
}
}
public class Letter implements Smoothable {
public double[] smooth(double[] data) {
//do something
return smoothedData;
}
}
Actually, the Smoothable interface does not need to be visible to the client, and the signature of the smooth method can be very different in the envelope and letter classes. The bridge pattern allows the client to use the smoothing classes without worrying about changes in the underlying implementation.
Finally, a disclaimer: I don't have Coplien's C++ book, which appears to be the definitive source on the letter/envelope idiom, so my answer may somewhat off the mark.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

 |
 |
 |
Re: Re: Can someone explain Letter/Envelope Topic: Patterns
Tudor Girba, May 11, 2001 [replies:8]
The Letter/Envelope idiom is, in my opinion, not quite a Bridge Pattern. Let me explain why:
1. The first thing is that the Letter extends the Envelope (in Coplien's view). This implies that the Letter and The Envelope have the same interface, while in the Bridge Pattern the Abstraction and the Implementor don't.
2. The second thing is that while the idiom's intent is to provide just a placeholder, the Bridge Pattern's intent is to decouple an abstraction from it's implementation.
That is why I would say that the Envelope/Letter idiom is much more similar to the Proxy Pattern (in it's smart reference form).
Note: I do not really like the this idiom because the Envelope is aware about it's subclasses(Letter) and that is not a really healthy thing while doing object-oriented related stuff:). That is why I prefer the Proxy Pattern.
Have a nice day, Tudor
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

 |
 |
 |
 |
Re: Re: Re: Can someone explain Letter/Envelope Topic: Patterns
Michael Wax, May 13, 2001 [replies:7]
I don't disagree with you; the letter/envlope idiom is not identical to the bridge pattern. However, given the question of a Java equivalent to a C++ idiom, I don't think that it is far off. Part of my thinking is based on the equivalency of the bridge and handle/body patterns, and the fact that letter/envelope derives from handle/body.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

 |
 |
 |
 |
 |
 |
 |
 |
Re[2]: Can someone explain Letter/Envelope Topic: Patterns
Brad Peabody, Aug 4, 2006
From what I can tell, if you implemented Letter/Envelope in Java directly you'd end up with this:
/** envelope class just provides "interface"
and delegates to another object */
public class Smoother {
private Smoother subclassedSmoother;
public Smoother(Smoother aSubclassedSmoother) {
subclassedSmoother = aSubclassedSmoother;
}
public double[] smooth(double[] aDoubleArray) {
return smoother.smooth(aDoubleArray);
}
}
/** letter class provides implementation */
public class XyzSmoother extends Smoother {
public XyzSmoother() { super(null); }
public double[] smooth(double[] aDoubleArray) {
// implement XYZ smoothing algorithm...
return myNewDoubleArray;
}
}
And you'd use it by doing:
Smoother mySmoother = new Smoother(new XyzSmoother());
// pass mySmoother on to clients to use
But it seems that the crux of matter is that you wouldn't bother doing this in Java. You get the exact same behavior by doing this:
/** just use a Java-language "interface" */
public interface Smoother {
public double[] smooth(double[] aDoubleArray);
}
/** and implement it */
public class XyzSmoother implements Smoother {
public double[] smooth(double[] aDoubleArray) {
// implement XYZ smoothing algorithm...
return myNewDoubleArray;
}
}
The point that D Srinivasan made about having to recompile when implementations change is the most pertinent: In C++, changing the implementations can mean requiring what ever code is using them to recompile. The envelope/letter idiom addresses that. But the JVM doesn't have this issue, since the way it handles method calls internally never requires a recompile when implementations change - so you just simplify it down to the features of the Java language that fit the problem best.
From what I can tell that's why it's difficult to describe in terms of Java - it doesn't apply - it was created to solve an issue that doesn't exist in that language.
Is this item
helpful? yes no
Previous votes Yes: 1 No: 0
|
|

 |
 |
 |
 |
 |
Re[4]: Can someone explain Letter/Envelope Topic: Patterns
Ashish Pagey, Jan 3, 2003 [replies:2]
To confirm: GOF Design Patterns book describes Bridge to be also known as Handle/Body.
So that settles the issue that: Letter/Envelope is not same as Bridge. There is no one-to-one equivalent for Letter/Envelope in GOF patterns.
Proxy pattern addresses a completely different set of forces and in my opinion cannot be equated to Letter/Envelope.
With that established more on understanding letter/envelope:
This pattern should be used when you want different implementations for the same interface. This allows you to change the behaviour dynamically.
When using letter envolope chances are you will end up using Factory-method or Abstract Factory pattern to defer what class to instantiate.
This pattern however is *not* for adding new behaviour/attributes dynamically. For that one should use the decorator pattern.
-Ashish
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

 |
 |
Re: Can someone explain Letter/Envelope? Topic: Patterns
David Liu PREMIUM, Aug 5, 2002
There is a book call Patterns in Java by Mark Grand. I think it is a good complement to the original GOF book. I like the way the GOF explain the patterns; however, using Grand's book for it's UML and Java examples is very helpful.
Is this item
helpful? yes no
Previous votes Yes: 0 No: 0
|
|

 |
|
|
 |
|