ActionScript 3.0 Design Patterns
William B. Sanders and
Chandima Cumaranatunge
Beijing
•
Cambridge
•
Farnham
•
Köln
•
Paris
•
Sebastopol
•
Taipei
•
Tokyo
,title.3638 Page iii Tuesday, July 3, 2007 2:53 PM
ActionScript 3.0 Design Patterns
by William B. Sanders and Chandima Cumaranatunge
Copyright © 2007 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editor:
Steve Weiss
Developmental Editor:
Robyn G. Thomas
Production Editor:
Philip Dangler
Copyeditor:
Sohaila Abdulali
Indexer:
John Bickelhaupt
Cover Designer:
Karen Montgomery
Interior Designer:
David Futato
Illustrators:
Robert Romano and Jessamyn Read
Printing History:
July 20007: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. ActionScript 3.0 Design Patterns, the image of a rosy feather starfish, and related
trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors
assume no responsibility for errors or omissions, or for damages resulting from the use of the
information contained herein.
This book uses RepKover
™
, a durable and flexible lay-flat binding.
ISBN-10: 0-596-52846-9
ISBN-13: 978-0-59652846-1
[M]
,copyright.3756 Page iv Tuesday, July 3, 2007 2:53 PM
Adobe Developer Library, a copublishing partnership between O’Reilly Media Inc.,
and Adobe Systems, Inc., is the authoritative resource for developers using Adobe
technologies. These comprehensive resources offer learning solutions to help devel-
opers create cutting-edge interactive web applications that can reach virtually any-
one on any platform.
With top-quality books and innovative online resources covering the latest tools for
rich-Internet application development, the Adobe Developer Library delivers expert
training straight from the source. Topics include ActionScript, Adobe Flex®, Adobe
Flash®, and Adobe Acrobat®.
Get the latest news about books, online resources, and more at http://adobedeveloper
library.com.
vii
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xi
Part I. Constant Change
1. Object-Oriented Programming, Design Patterns, and ActionScript 3.0
. . . . .
3
The Pleasure of Doing Something Well 3
OOP Basics 10
Abstraction 11
Encapsulation 15
Inheritance 24
Polymorphism 34
Principles of Design Pattern Development 42
Program to Interfaces over Implementations 45
Favor Composition 49
Maintenance and Extensibility Planning 57
Your Application Plan: It Ain’t You Babe 60
Part II. Creational Patterns
2. Factory Method Pattern
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65
What Is the Factory Method Pattern? 65
Abstract Classes in ActionScript 3.0 68
Minimalist Example 69
Hiding the Product Classes 73
Example: Print Shop 74
Extended Example: Color Printing 80
Key OOP Concepts Used in the Factory Method Pattern 84
,asdpTOC.fm.3994 Page vii Tuesday, July 3, 2007 2:53 PM