jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Q Promoted 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



Answer this question    Manage message; promote etc...  Hide so not publicly visible


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



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible
Re: Re: Can someone explain Letter/Envelope
Topic: Patterns
Jackie Noorden, May 9, 2001
Many thanks Michael, that's just what I needed. Love and kisses, Jackie ;)

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible
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



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible
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



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible
Re: Re: Re: Re: Can someone explain Letter/Envelope
Topic: Patterns
Tudor Girba, May 16, 2001  [replies:3]

Of course you were not off. I did not mean that (although I appeared pretty rough :-)). All I was saying is that to me the idiom looks more like a Proxy because of the enforcement of the same interface.

Now, I re-read the handle/body pattern and it's intent is indeed to decouple and abstraction from its implementation. But, the envelope/letter seems to me to be a bit different in intent. I don't know, maybe I am wrong.

Best regards,
Tudor.



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible
Re: Re: Re: Re: Re: Can someone explain Letter/Envelope
Topic: Patterns
Jackie Noorden, Jun 4, 2001  [replies:2]
I guess this is getting to the crux of my confusion. I thought one of the purposes of the *envelope* was to allow some, say, *address* details to be added without changing the *letter/business object*. For instance, in a highly distributed environment one might want to add a timestamp recording when the instance was created. I imagined this being added to the *envelope* not the *letter*.

Regards,
Ms Jackie

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible

Re: Can someone explain Letter/Envelope
Topic: Patterns
D Srinivasan, Jun 22, 2001  [replies:1]
Actually I tend to think of the letter envelope paradigm also in terms of Coplien and Lakos.
In this view the envelope serves as the "interface" that does not change and the letter is contained - as opposed to derived from - the envelope. The envelope chooses to expose any or all of the implementation of the letter and should be considered as the contract with the client.
OK with that out of the way, what purpose does all this serve. There are two reasons that I know of. One is to insulate the client from imlementation changes in the letter (such as changing the container type implementation for performance reasons). The second reason is that in a C++ environment any change in the size of data members of a class requires a recompile of all clients using that class. However if the envelope only has a pointer to the letter (and this is a strict requirement) then any change in the letter implementation only requires a recompile of the letter itself, not of any clients using the envelope. This is achieved since you have strictly enforced that all access is via the envelope.

BTW, I have not had a reason to use this in a java environment so far since java does not have the static dependencies that C++ has.
/D

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible

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



Reply to this answer/comment    Manage message; promote etc...  Take off my 'to do' list  Hide so not publicly visible
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



Reply to this answer/comment    Manage message; promote etc...  Take off my 'to do' list  Hide so not publicly visible
Re[5]: Can someone explain Letter/Envelope
Topic: Patterns
James Lopes, Jul 31, 2005

I'm relatively new to Patterns and find this question about the envelope/letter idiom intresting. Intresting in that the question brings out the point made in the GOF book "Design Patterns Elements of Resusable Object-Oriented Software" that creating a design pattern is difficult. Here in this message string there is one person who has asked a question and four others who have strong opions about the correct answer. Each proclaims doubt about the previous answers correctness.

I can't tell you how much this message has helped. By the way the GOF has envelope/letter idiom as relating to the State pattern p313 near the bottom of the page.

Thanks to all.



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment    Manage message; promote etc...  Take off my 'to do' list  Hide so not publicly visible
Re[5]: Can someone explain Letter/Envelope
Topic: Patterns
Victor van Uitert, Oct 11, 2006
With the risk of adding to the confusion: the GOF Design Patterns book actually mentions Letter/Envelope, in the State pattern description (see under 'Knowns Issues').

It reads:

"Coplien's Envelope-Letter idiom is related to State. Envelope-Letter is a technique for changing an object's class at run-time. The State-pattern is more specific, focusing on how to deal with an object whose behevior depends on state."

So GOF does recognize Letter/Envelope do be a distinct pattern, with a function other than compilation optimization. They just didn't deam it important enough to enter in in their 'design pattern catalogue'.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment    Manage message; promote etc...  Take off my 'to do' list  Hide so not publicly visible

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



Reply to this answer/comment    Manage message; promote etc...  Hide so not publicly visible


Ask A Question



Related Links

Patterns FAQ

Patterns Forum

Design Patterns Home Page

Huston Design Patterns

Brad Appleton's Software Patterns Links

Object-Oriented Bibliography

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers