Framework Design Guidelines: Factored Types

Continuing in our weekly blog post series that highlights a few of the new image[5]_thumb[2]_thumb[2]_thumbadditions to the Framework Design Guidelines 2nd edition.. This content is found in the Factored Types section of Chapter 9: Common Design Patterns. Phil offers some great additions to the base pattern.

PHIL HAACK

Since Factored Types have an explicit lifetime, it probably makes good sense to implement the IDisposable interface so that developers can make use of the using statement. The code sample here could then be refactored to:

using(SerialPort port = new SerialPort("COM1")) {
   port.Open();
   GZipStream compressed;
   compressed = new GZipStream(port.BaseStream,
CompressionMode.Compress);
    compressed.Write(data, 0, data.Length);
}

Comments

  • Anonymous
    February 23, 2009
    PingBack from http://www.anith.com/?p=12905

  • Anonymous
    February 23, 2009
    Continuing in our weekly blog post series that highlights a few of the new additions to the Framework

  • Anonymous
    February 23, 2009
    And a factored type is ...? Cheers, Wes

  • Anonymous
    February 24, 2009
    Is there a specific reason why you don't use the using statement with the GZipStream?

  • Anonymous
    February 24, 2009
    It would be helpful if we knew what "the code sample here" was.  Having the "after" without the "before" is pretty meaningless.

  • Anonymous
    February 24, 2009
    I'm not familiar with what a "Factored Type" is either.  A google search did land me on this interesting patent, though: http://www.patentstorm.us/patents/7430732/description.html Which makes me wonder if I'm legally allowed to use them?

  • Anonymous
    February 24, 2009
    First time I've heard the term. @Daniel : Software patents are evil (and it's idiotic that they're granted), but patents on APIs are positively demented. Just saying, s'all.

  • Anonymous
    February 24, 2009
    The comment has been removed

  • Anonymous
    February 25, 2009
    The example code ignores that the gzip stream buffers data to allow its compression. For the example to work it should flush (or better yet: close) the GZipStream before closing the serial port. (A nested using would probably give the cleanest example)