The Artima Developer Community
Sponsored Link

Books Forum
Head First Design Patterns

21 replies on 2 pages. Most recent reply: Jan 16, 2007 6:41 AM by Glenn Puchtel

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 21 replies on 2 pages [ « | 1 2 ]
hoping

Posts: 1
Nickname: hoping
Registered: May, 2005

Re: Head First Design Patterns Posted: Jan 8, 2006 8:50 PM
Reply to this message Reply
Advertisement
Ravi, I quit agree with you!

Mark Jordan

Posts: 1
Nickname: markjjorda
Registered: Apr, 2006

Re: Head First Design Patterns Posted: Apr 12, 2006 12:42 PM
Reply to this message Reply
Hi... I read the book... very good read.

However, I am about to blog about the misuse of the decorator pattern, and I use the example in the book as the subject for this topic. I thought I'd post the content here for comment before I post on my blog.

=== Begin Content ===

Is it me, or is the decorator pattern misused? I keep seeing examples of the decorator pattern that make no attempt at providing all public interface methods and properties on the decorator class that are present in the type's public interface. This is wrong, in my opinion.

I recently read the book, "Head First Design Patterns" by Eric Freeman et al. The publisher provides a sample chapter from the book which so happens to be the subject design pattern. You can view the pdf of Chapter 3, "The Decorator Pattern", here: http://www.oreilly.com/catalog/hfdesignpat/chapter/index.html.

In this chapter, they have an example on page 102 whereby they attempt to create a decorator for the FilterInputStream class. They override 2 method interfaces for the "read" method, effectively decorating the subject FilterInputStream that was passed in during construction/instantiation of the decorator.

Here is a reprint of the code:

public class LowerCaseInputStream extends FilterInputStream {
public LowerCaseInputStream(InputStream in) {
super(in);
}
public int read() throws IOException {
int c = super.read();
return (c == -1 ? c : Character.toLowerCase((char)c));
}
public int read(byte[] b, int offset, int len) throws IOException {
int result = super.read(b, offset, len);
for (int i = offset; i < offset+result; i++) {
b = (byte)Character.toLowerCase((char)b);
}
return result;
}
}

And now my question… what about all of the other public methods and properties? They also need to be in the decorator class to effectively delegate the reference of them to the FilterInputStream object that we are encapsulating. For instance, where is the following in the decorator class (syntactical errors not important):

public void reset()
{
super.reset();
}

GoF clearly indicates that "a decorator objects interface must conform to the interface of the component it decorates." Thus, the example in this book is incorrect per GoF specification. There is no way that this decorator object could be seemlessly referenced in place of the object it decorates. Why? Take the lines of code below (again, disregard syntax for the moment):

filterInputStream = new FilterInputStream()
filterInputStream.reset();
filterInputStreamDecorator = new FilterInputStreamDecorator(filterInputStream);
filterInputStreamDecorator.reset();

The second line would execute fine. But what about the fourth line? Without the code I mentioned above that delegates the reset method on the decorator to the encapsulated object, what gets reset? -- Certainly not the encapsulated object!

Some might be tempted to argue that the patterns are only suggestive, and that variances could be allowed. However, I submit that by providing a decorator without all of the interface, you are merely creating a helper class. To wit, you might as well just create a normal class that does not inherit from the type of the encapsulated object.

Keywords: decorator pattern, Gang of Four, incorrect, wrong

Fireblaze .

Posts: 21
Nickname: fireblaze
Registered: Jul, 2006

Re: Head First Design Patterns Posted: Jul 6, 2006 10:58 AM
Reply to this message Reply
A good book translating the original academic "Design Patterns: Elements of Reusable Object-Oriented Software" into more easy-read-ready to use material. My suggestion is when you read and know "Head First Design Patterns" buy GoF - Design Patterns, so you get the deeper insight and tradeoff explained.

GoF - Desin Patterns has spawn dosens of new books explaining and simplifing it.

New to patterns, and dont like to read academical work? Buy head-first patterns. Otherwise by the original GoF - Design Patterns

http://www.amazon.com/gp/product/0201633612/104-9682356-3757537?v=glance&n=283155

Will Junior

Posts: 3
Nickname: willj
Registered: Sep, 2006

Re: Head First Design Patterns Posted: Sep 2, 2006 11:30 PM
Reply to this message Reply
Where can I buy this book in UK ?
_________________________________
American Express - Training Courses at UK Virtual College

Vincent O'Sullivan

Posts: 724
Nickname: vincent
Registered: Nov, 2002

Re: Head First Design Patterns Posted: Sep 11, 2006 4:36 AM
Reply to this message Reply
> Where can I buy this book in UK ?
> _________________________________
> American
> Express
-
> Training Courses
> at UK Virtual College


Amazon UK - Head First Design Patterns. As a virtual bookshop, it would seem to be the logical place to buy books for a virtual college.

dinilkumar Narayanapillai

Posts: 1
Nickname: dinilkumar
Registered: Nov, 2006

Re: Head First Design Patterns Posted: Nov 3, 2006 12:56 AM
Reply to this message Reply
> Kelley,
>
> We appreciate the kind words and hearing that the book is
> working for C++ developers.
>
> We'd love to hear your suggestions for future books as
> well.
>
> Best,
>
> Eric

Glenn Puchtel

Posts: 5
Nickname: gpuchtel
Registered: Aug, 2006

Re: Head First Design Patterns Posted: Jan 16, 2007 6:41 AM
Reply to this message Reply
I have recently released version 1.6 of the 'Silver' package at http://sourceforge.net/projects/hfdp-cpp/

These packages are C++ translations of the examples found in 'Head First Design Patterns' plus more; I have been added missing 'Left Over Patterns' as well. Visit the URL and release notes as well as readme files for more information

Glenn

Flat View: This topic has 21 replies on 2 pages [ « | 1  2 ]
Topic: Rails for Java Developers Previous Topic   Next Topic Topic: Google Maps API

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use