Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Decorator pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Motivation== [[Image:UML2 Decorator Pattern.png|thumb|400px|UML diagram for the window example]] As an example, consider a window in a [[windowing system]]. To allow [[scrolling]] of the window's contents, one may wish to add horizontal or vertical [[scrollbar]]s to it, as appropriate. Assume windows are represented by instances of the ''Window'' interface, and assume this class has no functionality for adding scrollbars. One could create a subclass ''ScrollingWindow'' that provides them, or create a ''ScrollingWindowDecorator'' that adds this functionality to existing ''Window'' objects. At this point, either solution would be fine. Now, assume one also desires the ability to add borders to windows. Again, the original ''Window'' class has no support. The ''ScrollingWindow'' subclass now poses a problem, because it has effectively created a new kind of window. If one wishes to add border support to many but not ''all'' windows, one must create subclasses ''WindowWithBorder'' and ''ScrollingWindowWithBorder'', etc. This problem gets worse with every new feature or window subtype to be added. For the decorator solution, a new ''BorderedWindowDecorator'' is created. Any combination of ''ScrollingWindowDecorator'' or ''BorderedWindowDecorator'' can decorate existing windows. If the functionality needs to be added to all Windows, the base class can be modified. On the other hand, sometimes (e.g., using external frameworks) it is not possible, legal, or convenient to modify the base class. In the previous example, the ''SimpleWindow'' and ''WindowDecorator'' classes implement the ''Window'' interface, which defines the ''draw()'' method and the ''getDescription()'' method that are required in this scenario, in order to decorate a window control.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Decorator pattern
(section)
Add topic