Home arrow Flash arrow Page 2 - A Simple Decorator Pattern Example
FLASH

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
TABLE OF CONTENTS:
  1. · A Simple Decorator Pattern Example
  2. · Abstract Decorator Class
  3. · Concrete Decorations
  4. · Wrapping Up

print this article
SEARCH DEVARTICLES

A Simple Decorator Pattern Example - Abstract Decorator Class
(Page 2 of 4 )

Next, the abstract decorator is a subclass class of the Component class that inherits the informationvariable, so nothing is needed as far as theinformation variable is concerned. In fact, nothing’s required for this simple example other than defining the class as an extension of theComponentclass. However, thegetInformation()method is re-implemented independently of theComponent class, using theoverridestatement—which does what it says on the tin; it overrides parent class methods. This is done to distinguish the same method being used for theDecoratorclass from the method being used for theComponent class. All the concrete decorations are subclassed from theDecoratorclass, and all concrete components are subclassed directly from theComponentclass. Further on, the concrete components will be wrapped in concrete decorators, and such distinctions become important in more complex implementations of the Decorator design pattern. Thetrace()statement is used to show you where in the process the abstract Decorator class appears. Save Example 4-2 asDecorator.as.

Example 4-2. Decorator.as

package
{
  
//Abstract Decorator in Decorator Design Pattern
  
//**************
  
//Abstract class
  
public class Decorator extends Component
  
{
     
trace("|*|Decorator|*|");
     
override public function getInformation():String
     
{
        
return information;
      }
   }
}

Once the two abstract classes,ComponentandDecorator, have been established, it’s time to work with the concrete classes. For this example, only a single concrete component is created. Cleverly namedConcreteComponent, this class represents whatever will be decorated in a Decorator design pattern. You can have multiple concrete components that all use the same set of decorations, or only a single one. Later in this chapter, you will see an application where multiple concrete classes are decorated by a single set of decorators. The nice thing about the Decorator is that you can add as many concrete components as you want. Imagine a business web site where the concrete component represents an e-business site that you’ve worked on for several months. Using a Decorator design pattern, you’ve developed several useful and tested elements that are applied using decorators. Shortly after the project is complete, you get a request to develop another site with a different main product and an overlapping set of elements. Rather than starting from scratch, all you have to do is to add a different concrete component class and some new decorators that aren’t available in the original set.

Example 4-3 shows that the concrete component does little more than extend theComponentclass and add a constructor. It inherits all of the features of the Component class, and uses the informationvariable to place a message in the Output window to point to the decorators. Save the code in Example 4-3 asConcreteComponent.as.

Example 4-3. ConcreteComponent.as

package
{
  
//Concrete Component
  
public class ConcreteComponent extends Component
  
{
     
public function ConcreteComponent()
     
{
        
//\u2794 is Unicode for a right-pointing arrow
        
information = "Concrete Component is decorated with \u2794";
     
}
   }
}

In Example 4-3, you see that Unicode is inserted using the format \u + character code value. In the example, an arrow character is formed using Unicode 2794. To search for a character you may want to use, see http://www.fileformat.info/info/unicode/ char/search.htm. You’ll find several different arrow characters you can use, including \u0363, \u2192, \u21aa, and \u21d2.

The next step is to build concrete decorator classes. In this abstract example, two concrete decorators will wrap themselves around the concrete component. So, to get started, we’ll need a Component instance variable to hold the component we’re wrapping.

  var components:Component;

The variable is namedcomponents, with an “s,” becausecomponentis a built-in word in ActionScript 3.0. This variable is referenced in the decorator’s methods. Next, we need a way to affix thecomponents variable to the object being wrapped. The following code shows how the component being wrapped is passed to the decorator’s constructor.

  public function DecConA(components:Component)
  {
    
this.components=components;
  }

Finally, you need a getter function to get the unique information from the concrete decorator. The public method inherited from theDecoratorclass must be set up using theoverridestatement. Here, the method gets both the concrete component’s information [components.getInformation()],and adds on that of the concrete decoration [+ " Decoration Alpha:"]:

  override public function getInformation():String
  {
    
return components.getInformation() + " Decoration Alpha:";
  }


blog comments powered by Disqus
FLASH ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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