Sunday 6 May 2007

Decorator Pattern (blog 8)

The next pattern which i am going to discuss briefly is Decorator Pattern. Its a pattern which allows an elegant OOP technique to add some extra responsibilities or you can say functionalities to a specific object of a class. You can add functionalities to all the objects or even a single object of a entire class.
Decorator pattern is also know as Wrapper, is an object which has a interface similar to an object that it consist.
Decorator changes the way an object appears that is totally different from subclassing.
Drawback
The component which is enclosed with decorator is not the same. Tests of object types may fail in this case. And there is another point about this pattern which is not impressive is by using this pattern ! lots of small objects are created which is severe stress to the programmer when it comes to debugging some error.[1]
Reference:
C# Design Patterns: The Decorator Pattern

2 comments:

Neo Milupi said...

Hi Hani,

well explained!

Just in addition,

i would say... the decorator pattern is a means to transparently adding responsibilities to another object.

In some cases, it's a simpler solution than creating additional subclasses through inheritance. and the implementation of it is by creating a class that wraps access to another class and implements the exact same public interface as its inner member.

Cheers!
Wamuneo

Unknown said...

Hi Hani

When you say “you can add functionality to all the objects or even a single object of a entire class” it doen not make sense, also when you say that “is an object which has a interface similar to an object that it consist.” Is not a valid statement.

Decorator changes the way an object appears that is totally different from subclassing is yet again another wrong statement, because decorator does almost the same as sub classing, the difference between two is that the sub class adds functionalities at compilation time while decorator does it at run time.

Best of luck

Waseem