Home arrow Flash arrow Using the Decorator Pattern for a Real Web Site

Using the Decorator Pattern for a Real Web Site

In this conclusion to a six-part series on the decorator pattern, we're going to put what we've learned to work on a more realistic example than the ones we've covered up until now: a web site for a hybrid car dealership. 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 / 5
December 31, 2008
  1. · Using the Decorator Pattern for a Real Web Site
  2. · Hybrid car classes concrete component
  3. · Using Auto Options as Decorators
  4. · Setting Up the User Interface
  5. · Creating the document and setting the stage
  6. · Implementing the concrete components and their decorators

print this article

Using the Decorator Pattern for a Real Web Site
(Page 1 of 6 )

Dynamic Selection of Concrete Components and Decorations: A Hybrid Car Dealership

Up to this point, the examples have focused on different dimensions of the Decorator design pattern, with the emphasis on how the different elements in the Decorator design pattern can be used with different components and decorations. Both concrete and abstract output has shown different ways to display information, but no example has illustrated how to input data dynamically. This Decorator example uses the case of selecting automobiles and their options to illustrate how to dynamically input data for both decorators and concrete components.

Imagine that you are responsible for creating and maintaining a web site for a car dealership. With each year, new models appear, and different options are made available. You never know what options are going to be added or dropped, or even if the same car models will be around from one year to the next. Youíve decided to use the Decorator pattern because you can easily add or drop both concrete components and concrete decorators. The concrete components will be the different models, and the options are the different decorations for any model selected. So whenever a new model appears, you simply update the concrete component to reflect those changes. Likewise with the options available for any of the models, all you need to change are the concrete decorations. You can easily add or change decorations without altering the programís basic structure.

Setting Up the Hybrid Car Components

This particular dealership has decided to specialize in four hybrid model cars. This example uses four such cars, the Prius, Mercury Mariner, Ford Escape and Honda Accord hybrids. So in addition to an abstract Component class, this example requires four concrete components.

Auto abstract component

To get started, the abstract component is cast as a class named Auto. It needs only a string for the name of the car and a numeric variable for the carís price. Two getter functions for auto type and price make up the rest of the abstract component. Example 4-38 shows the code saved asAuto.as.

Example 4-38. Auto.as

//Abstract class
public class Auto
protected var information:String;
protected var bucks:Number;

      public function getInformation():String
return information;
public function price():Number
return bucks;

As with all Decorator design patterns, all other classes are derived from this abstract component class. In the next section, youíll see that all of the concrete component classes are subclasses of theAutoclass.

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-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials