#7 Decorator Pattern

#7 Decorator Pattern - Decorator Pattern One of the most...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Decorator Pattern One of the most important aspects of the development process that developers and programmers have to grapple with is change, which is why design patterns were introduced in the first place. In particular, design patterns are intended to help you handle change as you have to adapt your code to new and unforeseen circumstances. Developers spend much more time extending and changing code than they do originally developing it Closed for modification but open for extension The Decorator pattern allows you to write your code and avoid modification, while still extending that code if needed. As much as possible, make your code closed for modification, but open for extension. In other words, design your core code so that it doesnt have to be modified a lot, but may be extended as needed. Role The role of the Decorator pattern is to provide a way of attaching new state and behavior to an object dynamically. The object does not know it is being decorated, which makes this a useful pattern for evolving systems. A key implementation point in the Decorator pattern is that decorators both inherit the original class and contain an instantiation of it. Illustration As its name suggests, the Decorator pattern takes an existing object and adds to it. As an example, consider a photo that is displayed on a screen. There are many ways to add to the photo, such as putting a border around it or specifying tags related to thecontent. Such additions can be displayed on top of the photo, as shown in Figure. The combination of the original photo and some new content forms a new object. In the second image shown , there are four objects: the original photo as shown to the left, the object that provides a border, and two tag objects with different data associated with them. Each of them is a Decorator object. Given that the number of ways of decorating photos is endless, we can have many such new objects. The beauty of this pattern is that The original object is unaware of any decorations. There is no one big feature-laden class with all the options in it. The decorations are independent of each other. The decorations can be composed together in a mix-and- match fashion. Design Now, we can specify the players in the Decorator pattern in a UML diagram. The essential players in this UML diagram are: Component - An original class of objects that can have operations added or modified (there may be more than one such class) Operation - An operation in IComponent objects that can be replaced (there may be several operations)...
View Full Document

This note was uploaded on 06/14/2011 for the course COMPUTER 091 taught by Professor Rajivsir during the Summer '11 term at MIT.

Page1 / 31

#7 Decorator Pattern - Decorator Pattern One of the most...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online