Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / C#
Article

Decorator Pattern in .Net

Rate me:
Please Sign up or sign in to vote.
1.13/5 (13 votes)
14 Feb 2008CPOL1 min read 26.7K   11   6
Decorator Pattern in .Net
Decorator Pattern is a structural pattern. It is used to attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. Decorator pattern is used in .Net. The following collection classes are the decorators of the ArrayList class:
  1. SyncArrayList: This denotes a synchronized arraylist which can be used multithreading scenario. This arraylist is to be used when multiple threads are to modify a single instance of arraylist.
  2. FixedSizeArrayList: This denotes a fixed size arraylist. The size of the array list cannot be changed. Elements cannot be added and removed. Existing elements can be replaced.
  3. ReadOnlyArrayList: This denotes a read-only arraylist. Elements cannot be added and removed. Existing elements cannot be replaced.

All the above decorators are defined as following:

private class [Sync/Fixed/ReadOnly]ArrayList : ArrayList

{

internal [Sync/Fixed/ReadOnly]ArrayList(ArrayList arrayList) {...}

}

The above structure is the proof of all the aboved mentioned classes to be a deocorator of the ArrayList class.

All the above deocorators are private and can be instantiated using the static methods provided on ArrayList class:

All the decorators override appropriate virtual methods and adds the additional behavior. Below shows the sample code snippet for the Synchronized arraylist the decorators:

Similarly ReadOnlyArrayList overrides the virtual method of the ArrayList class and throws exception to restrict any modifications in the list.

In a similar manner FixedSizeArrayList overrides the virtual method of the ArrayList class and throws exception to restrict any changes to the size of the list.

This is how the decorator pattern is being used in .net framework. There are possibilities of decorator pattern being used at multiple places in the framework. It can be found out by studying the framework thoroughly and understanding the patterns used in the framework.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralDelete this Pin
Bert delaVega15-Feb-08 8:24
Bert delaVega15-Feb-08 8:24 
GeneralRe: Delete this Pin
Pratik.Patel26-Sep-08 22:25
Pratik.Patel26-Sep-08 22:25 
I would love to know the reason for "Complete garbage" because I don't feel the same.

Thanks and regards,
Pratik Patel

QuestionCopied Content? Pin
Justin Perez15-Feb-08 3:12
Justin Perez15-Feb-08 3:12 
AnswerRe: Copied Content? Pin
Anand Patel15-Feb-08 4:02
Anand Patel15-Feb-08 4:02 
GeneralRe: Copied Content? Pin
Philip J. Smith15-Feb-08 5:00
Philip J. Smith15-Feb-08 5:00 
GeneralRe: Copied Content? Pin
Anand Patel17-Feb-08 18:34
Anand Patel17-Feb-08 18:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.