Decorators Design Pattern
In the last days I have been designing some programs. I will have to do it in the master program, since we’re going to change a lot of the program made so far. So I decide to study some O.O. and nothing better than Design Pattern to improve my O.O. expertise. Today I want talk some about the Decorator Design Pattern.
Everybody that have already read some advanced O.O. articles knows about the nasty characteristics of the inheritance[1][2]. I don’t agree with all these, but when we start to study we’re more able to understand their points. The over uses of inheritance leads us to less flexible and maintainable designs. Other point is that we will have static behavior (compile time implemented) and using Composition we can have dynamic behavior.
Dynamic behavior could be mandatory in some design or you would be crazy in a big number of classes. For instance, if you have a Window class with Horizontal Bars, Vertical Bars, both or none. You could create a class for each case, but is not difficult to imagine that this problem could be large and leads to a impossible number of classes.
Let’s see the solution using decorators First we’ll start with a diagram of the Decorators Design pattern
Using this design pattern you can create objects that combine the others behavior for example if I want a object with both scroll bars:
Widget* aWidget = new HorizontalScrollBarDecorator ( VerticalScrollBarDecorator( SimpleWindow()));
This pattern is used also in the JAVA API. Look the InputStream Implementation [4 – Decorators subsection].
This post it is not too much useful alone, but it has good references…. If is necessary comment it.
1 – Inheritance is evil, and must be destroyed: part 1 – http://www.berniecode.com/writing/inheritance/
2 – Why extends is evil – http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html
3 – Decorator pattern – http://en.wikipedia.org/wiki/Decorator_pattern
4 – Solve the date-selection problem once and for all – http://www.javaworld.com/javaworld/jw-07-2003/jw-0704-toolbox.html
-
Arquivos
- Dezembro 2008 (2)
- Novembro 2008 (5)
- Agosto 2008 (1)
- Abril 2008 (2)
- Novembro 2007 (2)
- Setembro 2007 (1)
- Agosto 2007 (4)
-
Categorias
-
RSS
Entradas RSS
Comentários RSS