Home arrow Flash arrow Page 4 - A Simple Decorator Pattern Example

A Simple Decorator Pattern Example

In this second part of a six-part series on the Decorator design pattern, you will walk through an example that shows you the minimum number of components required to create it. This article is excerpted from chapter four of ActionScript 3.0 Design Patterns Object Oriented Programming Techniques, written by William B. Sanders and Chandima Cumaranatunge (O'Reilly, 2007 ISBN: 0596528469). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
December 04, 2008
  1. · A Simple Decorator Pattern Example
  2. · Abstract Decorator Class
  3. · Concrete Decorations
  4. · Wrapping Up

print this article

A Simple Decorator Pattern Example - Wrapping Up
(Page 4 of 4 )


To execute the Decorator design pattern, the whole key lies in knowing how to wrap a component in a concrete decorator. First, you need to instantiate a concrete component.

  var testComponent:Component = new ConcreteComponent();

Then, you wrap the component in one or more decorations using the following format:

  componentInstance=new ConcreteDecorator(testComponent);

So, in our example, with two concrete decorations, we’d write:

  testComponent=new DecConA(testComponent);
  testComponent=new DecConB(testComponent);

At this point,testComponentis decorated with two decorations. We could duplicate the above lines adding the same two decorations as often as we wanted. Think of the decorations as red and green Christmas tree ornaments. The tree could be covered with nothing but red and green ornaments, rather than just one of each. The Decorator design pattern is employed cumulatively. That is, as you add each decoration, it’s added to those already wrapping the concrete component.

Finally, to see what havoc we’ve wrought, we use the getter method—getInformation():


To see how all of this works, save Example 4-6 asDecTest.asin an ActionScript file. Then open a new Flash document file, and type DecTest in the Document class window in the Properties panel.

Example 4-6. DecTest.as

   import flash.display.Sprite;
   public class DecTest extends Sprite
public function DecTest()
         //Instantiate Concrete Component
         var testComponent:Component = new ConcreteComponent();
         //Wrap first decorator around component
         testComponent=new DecConA(testComponent);
         //Wrap second decorator around component
         testComponent=new DecConB(testComponent);
         //Output results

Figure 4-5 shows what you should see in your Output window when you test the movie.

Figure 4-5.  Decorations on component

To understand what’s going on in the Decorator pattern, go back and look at Figure 4-2. The example application first instantiated aConcreteComponent()object. That object displays a message pointing to its decorations. Imagine that object (testComponent) as the smallest can to the far left in Figure 4-2. That can is then placed into decorator Can #1. At this point, the concrete component object (testComponent) is decorated with Can #1, but retains its original properties –much in the same way that a lawn decorated with a family of gnomes still retains its property of green grass. Next, Can #1, which now contains the concrete component, is dropped into Can #2. Now Can #2 has both Can #1 and the Concrete component Can. Thus, Can #2 has all of the properties of itself plus those of the cans inside.

In a sense, the whole process works like the compound operator, plus-equal (+=). Each decorator attaches itself to the existing component and its decorator. As each decorator is added, all the previous ones are retained but not duplicated, unless you add the same decorator more than once. So, as the output shows, you can add as many decorations as you want simply by wrapping them in the existing object in one of the decorators.

Please check back next week for the continuation of this article.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials