2

Actually i have a presentation on decorator design pattern. I am sure i have used it couple of times in java and c#, the purpose to ask this is that I am not getting some real world simple examples by which I can easily present my audience about that. Can anyone help me out?

sab669
  • 3,984
  • 8
  • 38
  • 75
  • 1
    http://en.wikipedia.org/wiki/Decorator_pattern – sab669 Nov 14 '13 at 21:16
  • 1
    http://stackoverflow.com/questions/2707401/please-help-me-understand-the-decorator-pattern-with-a-real-world-example. Look into java.io.FileInputStream and java.io.BufferedInputStream for examples. http://stackoverflow.com/questions/6366385/decorator-pattern-for-io – user1339772 Nov 14 '13 at 21:21

3 Answers3

1

You can find an accepted answer here.

However in case you don't understand the answer at that link, here is a simple illustration that I thought of about how real world decorations work. As stated by the name, decorator is like a decoration in real world.

Imagine a big ball. You want to make a ball with some camo pattern and some patterns and stickers attached. Should be simple enough.

Ball Factory

At first you will have a Ball Factory (or creator) which produce the ball with black color (or transparent, doesn't matter). Trivial enough.

RedColorBall Factory

You want the ball has a red base color. So after the Ball Factory finisihed the ball, then the RedColorBall Factory will paint it red, and return it to you. Again it is trivial enough.

BluePatternBall Factory

After some days you think the ball produced need to have a blue camo pattern. so after the RedColorBall Factory return the red ball, the BluePatternBall Factory will paint a blue pattern and return it to you.

GreenStripeBall Factory

After some days you think the ball need to have a green stripe along with red base color (it's weird, don't ask). But it needs to be painted right after red base color are painted, and before the blue pattern are painted. So you make the GreenStripeBall Factory and positioned it after RedColorBall Factory and before BluePatternBall Factory.

ChicagoBullsSticker Factory

Then at last you need a chicago bulls (random choice, don't judge me) sticker at the ball. Then you make the ChicagoBullsStickerFactory, and put it at the last after BluePatternBallFactory. Then you get the red-based with green stripe and blue camo pattern ball with chicago bulls sticker.

Later on if you need to add some patterns or other stickers attached to balls, you only need to create another factory and put it on before/after or between them.

Community
  • 1
  • 1
Fendy
  • 4,565
  • 1
  • 20
  • 25
0

I/O Streams is the classic example in both languages for the Decorator pattern

atomaras
  • 2,468
  • 2
  • 19
  • 28
0

The intent of the Decorator pattern is to let you extend an object's behavior dynamically by "wrapping itself" around the original object's type.

In the .NET Framework, the common example of this "wrapping" is the Stream class and all of its variants, like BufferedStream and CryptoStream.

The BufferedStream extends the Stream class by reading and writing large chunks of data bytes for better performance than a normal Stream; while CryptoStream extends the Stream class by encrypting and decrypting data bytes on the fly for security. Both are still streams, because they can do everything a stream can do, like read, write, seek, etc.

Decorator classes usually have a constructor with an argument that represents the type they intend to decorate, for example:

new BufferedStream(Stream stream);
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80