Flash
  Home arrow Flash arrow Page 4 - The Decorator Pattern in Action
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

The Decorator Pattern in Action
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2008-12-11

    Table of Contents:
  • The Decorator Pattern in Action
  • Decorator Class: Dressing the Dolls
  • Concrete decorator classes
  • Implementing the Paper Doll Decorator

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    The Decorator Pattern in Action - Implementing the Paper Doll Decorator


    (Page 4 of 4 )

    As noted, no interface was created for implementing this application, and so you can see clearly how the concrete component is wrapped by the decorators. Then, on the stage, you can see the effects of the different combinations.

    The sequence is:

    1. Instantiate a concrete component.
    2. Wrap the concrete component in the desired decorator instance using the format:

        componentInstance = new DecoratorInstance(componentInstance) 
    3. Apply the getter method (getDressed()) to the concrete component instance initiated to get the fully wrapped values. The values will include those of the concrete component instance and the applied decorations.

    Example 4-16 illustrates how to implement the paper doll decorator.

    Example 4-16. FashionShow.as

    package
    {
       import flash.display.Loader;
       import flash.net.URLRequest;
       import flash.display.Sprite;
       public class FashionShow extends Sprite
       {
         
    var ensemble:Array=new Array();
          public function FashionShow()
          {
            
    trace("||--Working--||");
             var doll:Model = new Sue();
             doll=new Hat(doll);
             doll=new OrangeDress(doll);
             //doll=new BlueDress(doll);
             //doll=new Umbrella(doll);
             doll=new Bow(doll);
             doll=new Muff(doll);
             var ready2wear:String=doll.getDressed();
             sewingMachine(ready2wear);
             for (x=0; x < ensemble.length; x++)
             {
               
    clothesOn(ensemble[x]);
            
    }
          }
          private function sewingMachine(wardrobe:String):Array
          {
            
    ensemble=wardrobe.split("~");
             ensemble.shift();
             return ensemble;
         
    }
          private function clothesOn(outfit:String)
          {
            
    var clothier:Loader=new Loader();
             var item:String="clothes/" + outfit + ".gif";
             var getItem:URLRequest=new URLRequest(item);
             clothier.load(getItem);
             this.addChild(clothier);
          }
       }
    }

    Once you’ve got everything ready to go, you’ll need some GIF files. The model (concrete component) should be an image of a doll that you will clothe with the Decorator pattern. Each filename needs to be the name of the value assigned to thewhatToWearvariable in the concrete component or decorator listing, minus the tilde (~) character. For example, the concrete component,Sue,whatToWearvariable is assigned the string value “~sue” and so the GIF filename for that component would besue.gif. The decoratorBlueDresshas awhatToWearvalue of “~bluedress,” and so the image with the blue dress would be namedbluedress.gif. Place all the GIF files, including the file representing the doll model, in a folder namedclothes.

    Finally, open up a new Flash document and set the stage dimensions to 300 × 650. In the Properties panel’s Document class window, type in FashionShow. The size and shape of your stage will depend on the size of the paper dolls you use.

    Figure 4-6 shows two different combinations of decorations on the paper doll. The image on the left decorates with the hat, bow, orange dress, and muff, while the second is the blue dress, the bow and an umbrella. Each appears very different just by changing the combination of decorations.


    Figure 4-6.  Paper doll with two different decoration sets

    Instead of paper dolls, any component could be decorated with any characteristics. Also, you can include more than a single property or method in a decoration. In the next section, we’ll take a look at adding additional properties with the Decorator design pattern.

    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.

       · This article is an excerpt from the book "ActionScript 3.0 Design Patterns Object...
     

    Buy this book now. 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). Check it out today at your favorite bookstore. Buy this book now.

    FLASH ARTICLES

    - 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
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek