Click here to Skip to main content
13,296,823 members (35,072 online)
Click here to Skip to main content
Add your own
alternative version


11 bookmarked
Posted 14 Feb 2008

Decorator Pattern in .Net

, 14 Feb 2008
Rate this:
Please Sign up or sign in to vote.
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.


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


About the Author

Anand Patel
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDelete this Pin
Bert delaVega15-Feb-08 9:24
memberBert delaVega15-Feb-08 9:24 
GeneralRe: Delete this Pin
Pratik.Patel26-Sep-08 23:25
memberPratik.Patel26-Sep-08 23:25 
QuestionCopied Content? Pin
Justin Perez15-Feb-08 4:12
memberJustin Perez15-Feb-08 4:12 
AnswerRe: Copied Content? Pin
Anand Patel15-Feb-08 5:02
memberAnand Patel15-Feb-08 5:02 
GeneralRe: Copied Content? Pin
Philip J. Smith15-Feb-08 6:00
memberPhilip J. Smith15-Feb-08 6:00 
GeneralRe: Copied Content? Pin
Anand Patel17-Feb-08 19:34
memberAnand Patel17-Feb-08 19: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 15 Feb 2008
Article Copyright 2008 by Anand Patel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid