Home arrow Flash arrow Page 3 - Using Concrete Decorator Classes

Using Concrete Decorator Classes

In this fifth part of a six-part series on the decorator pattern, we create a whimsical example involving good and evil to show you concrete decorators in action. 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: 5 stars5 stars5 stars5 stars5 stars / 4
December 24, 2008
  1. · Using Concrete Decorator Classes
  2. · Deadly sins
  3. · Implementing the Good and Evil Decorator
  4. · Charting souls

print this article

Using Concrete Decorator Classes - Implementing the Good and Evil Decorator
(Page 3 of 4 )

Instead of a single implementation, you can try out two different implementations. The first one dresses up the two different concrete components, and sends the results to the output window. The second takes the results, uses them to place movie clips on a “soul graph,” and adds a label to an angel or devil movie clip—depending on whether good or evil is predominant.

Dual implementation

The first implementation decorates both the Dick andJaneconcrete components. This one is set up to use all 14 deadly sins and heavenly virtues, but you can use any combination you want. As each concrete component (lifeandlight) is wrapped, the good and evil properties are incremented or decremented, depending on which decorator wraps the concrete component. The reference to thecomponentsobject in each of the decorators is a reference to the concrete component being wrapped. With each new decorator, then, the value goes up and down depending on the wrapping decorator. Save Example 4-36 asMainDual.asin an ActionScript file. Open a new Flash document file, and save it as aDual.fla. Type in MainDual in the Document class window in the Dual.fla Properties panel, and resave it as MainDual.as. Now test the movie.

Example 4-36. MainDual.as

   import flash.display.Sprite;
   public class MainDual extends Sprite
public function MainDual()
         var life:Component=new Dick();
         var light:Component=new Jane();
         //***Add Good
         life=new Courage(life);
         light=new Hope(light);
         light=new Compassion(light);
         life=new Openness(life);
         life=new Diligence(life);
         light=new Justice(light);
         //***Add Evil
         light=new Malice(light);
         life=new Prejudice(life);
         life=new Dogmatisms(life);
         life=new Arrogance(life);
         life=new Indifference(life);
         life=new Rage(life);
         light=new Obsfuscation(light);
         trace(life.getSoul()+"\nTotal Virture: "+ 
         trace(light.getSoul()+"\nTotal Virture: "+

The output for all of the good and evil together is:

  Dick's soul 
  |Courage|Openness|Diligence|Prejudice| Dogmatisms|Arrogance
  Total Virture: -38
  Jane's soul 
  |Hope|Compassion|Justice|Malice| Obsfuscation
  Total Virture: 10

Because thetrace()statement subtracts theevil()method from thegood()method total value, any positive results indicate an abundance of good characteristics, and a negative result shows a plethora of negative traits.

You can “multi-wrap” using the same decorator more than once on the same concrete component. (Someone who thinks he can get the month-behind project done on time by not sleeping for a week is doubly hopeful, has a triple dose of diligence, and perhaps a double dose of arrogance.)

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