Common Bits&Bytes Patterns - Decorator Pattern, Part II (2 of 2)

Continued from http://dotnet.org.za/willy/archive/2008/06/23/common-bits-amp-bytes-patterns-composite-pattern-part-ii-1-of-2.aspx.

Category Structural design pattern
Intent

Attach additional behaviour or capabilities to an object dynamically.

Applicability

Use this pattern to:

  • Add responsibility to object dynamically
  • Allow withdrawal of responsibility
  • Avoid explosion of subclasses

Class Diagram

image

Source Code Example

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Text;
   5:  
   6: namespace DecoratorPattern
   7: {
   8:     public interface IComponent
   9:     {
  10:     }
  11:  
  12:     public interface IDecorator
  13:     {
  14:     }
  15:  
  16:     public class Reader_Component : IComponent
  17:     {
  18:     }
  19:  
  20:     public class InputStreamReader_Decorator : IDecorator, IComponent
  21:     {
  22:         public InputStreamReader_Decorator() { }
  23:         public InputStreamReader_Decorator(System.IO.FileStream inputStream)
  24:         {
  25:             this.inputStream = inputStream;
  26:         }
  27:         System.IO.FileStream inputStream;
  28:     }
  29:  
  30:     public class BufferedReader_Decorator : IDecorator, IComponent
  31:     {
  32:         public BufferedReader_Decorator(Reader_Component reader)
  33:         {
  34:             this.reader = reader;
  35:         }
  36:  
  37:         Reader_Component reader;
  38:     }
  39:  
  40:     public class FileReader_ConcreteDecorator : InputStreamReader_Decorator
  41:     {
  42:         
  43:     }
  44:  
  45:     public class CommsReader_ConcreteDecorator : InputStreamReader_Decorator
  46:     {
  47:     }
  48:  
  49:  
  50: }

Code Complexity Code Metrics

image

image

This has been the last of the pattern mini-series for the time being. I hope that these seven posts have been of some value to the community ... for me the formal documentation has allowed me to explore them in more detail and to create an interesting environment for my Unisa studies.

Published Monday, June 23, 2008 5:29 PM by willy

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: