Flash
  Home arrow Flash arrow Page 2 - 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 - 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:";
      }

    More Flash Articles
    More By O'Reilly Media


       · 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