Flash
  Home arrow Flash arrow Page 4 - A Simple Decorator Pattern Example
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

A Simple Decorator Pattern Example
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-12-04

    Table of Contents:
  • A Simple Decorator Pattern Example
  • Abstract Decorator Class
  • Concrete Decorations
  • Wrapping Up

  • 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


    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():

      trace(testComponent.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

    package
    {
       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
             trace(testComponent.getInformation());
         
    }
       }
    }

    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.

       · 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-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT