Click to See Complete Forum and Search --> : Question about Decorator design pattern


typewriter
February 29th, 2008, 06:17 PM
OK, assume an example of a window class, which can be passed to a decorator class for adding scrollbars and/or a decorator for adding a border.

What is the advantage of using the decorator pattern, as opposed to building those bits of functionality into the main window class, and simply turning them on or off as necessary?

boudino
March 3rd, 2008, 02:57 AM
Because it allows you to use scrollbars even with those controls, which doesn't implement the functionality themselves.

typewriter
March 3rd, 2008, 11:43 AM
Oh I see - so its not so useful on a single class like Window, but if you had Window, PopupWindow, SpecialWindow, etc you could avoid duplicate code by creating a decorator class that could extend any type of window (probably through a "Window" interface).