O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Preface

As ActionScript has evolved from a few statements in Flash to a full-fledged Internet programming language in the latest release of Flash and Flex, we have the ability to implement sophisticated designs developed for languages using object-oriented programming (OOP). ActionScript 3.0 heralds a new era in Flash and Flex programming because it implements the ECMAScript standard for Internet languages. Many of the features found in languages such as C++ and Java™ are now available in ActionScript 3.0.

Along with more sophisticated features in ActionScript 3.0 come more sophisticated ways of programming and thinking about programming. Most readers of this book will be familiar with OOP to some degree, and like the step from sequential or procedural programming to OOP, the step to design pattern programming is a step up for OOP programmers. We felt that because ActionScript 3.0 had arrived at a point where more complex and challenging programming structures can be developed, Flash and Flex programmers needed to understand programming techniques to cope with these structures.

By understanding design pattern programming, you will be able to write better OOP code, and reuse that code in other programs. The better paying positions in the programming field favor those developers who can work with team development, and the sophisticated structures that design patterns and OOP were developed to handle. At the same time that you gain proficiency in programming more complex applications, design patterns actually make programming easier. In large and complex applications, programmers have most difficulty with poor planning and awkward design structures. Design patterns not only provide solutions for common challenges, but also focus on maintenance and change. The vocabulary of design patterns is equally important because with it, you can become part of the developer community that communicates clearly in the language of design patterns.

Who this Book is for

We wanted to develop a book for intermediate to advanced ActionScript 3.0 users. Unlike some languages, such as Java, where the readers are likely to have computer science or computer engineering degrees, most ActionScript 3.0 users are likely to have learned ActionScript in developmental stages using Flash. As a result, their backgrounds are far more varied, and the programming base less definite. We’re sure that a number of ActionScript programmers have computer science or related backgrounds, and much of the introductory materials in the first chapter will be redundant. Likewise, we’re certain that some readers on the lower end of the intermediate level are learning object-oriented programming at the same time that they’re trying to pick up design patterns; they may have little or no formal training in programming.

Given the range of ActionScript programming backgrounds, we’re bound to be too difficult for some and too simplistic for others. However, this book’s overall goal is to explain how to use different design patterns. We targeted whom we considered intermediate level ActionScript developers. We’ve provided everything the intermediate level developer will need to move to the advanced level. For the advanced user, we provide explanations and examples of how to use design patterns with ActionScript 3.0.

How this Book is Organized

The book’s organization reflects the topic organization found in Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (Addison-Wesley, 1995).

The first part contains Chapter 1, which is an introduction to design patterns. We added an introductory section for readers with minimal object-oriented programming experience. More advanced users may want to skip the review of OOP, but go over the materials on design patterns.

Part I: Why Design Patterns?

Chapter 1, Core Concepts

Parts II, III and IV are the three major parts of the book. They examine fundamental design patterns, and organize the patterns into creational, structural and behavioral categories. Representative design patterns are included in each part, but we didn’t include every single design pattern from the book by Gamma and his associates.

Part II: Creational Patterns

Chapter 2, Factory Method Pattern

Chapter 3, Singleton Pattern

Part III: Structural Patterns

Chapter 4, Decorator Pattern

Chapter 5, Adaptor Pattern

Chapter 6, Composite Pattern

Part IV: Behavioral Patterns

Chapter 7, Command Pattern

Chapter 8, Observer Pattern

Chapter 9, Template Method Pattern

Chapter 10, State Pattern

Chapter 11, Strategy Pattern

Part V contains two chapters on using multiple design patterns in application development. The Model-View-Controller and Symmetric Proxy designs incorporate more than a single design pattern. They’re organized like the other chapters on design patterns as far as explaining how the multiple patterns work. However, the multiple designs rely more on object diagrams than class diagrams.

Part V: Combined Patterns

Chapter 12, Model-View-Controller Design Pattern

Chapter 13, Symmetric Proxy Pattern

Each chapter on design patterns is organized to optimize and clarify understanding the purpose of a design pattern and how to use it. The following outline can be found in each of the chapters on design patterns:

  • What is the pattern?

  • Key features of the pattern

  • The formal model of the pattern including a class diagram

  • Key OOP concepts found in the pattern

  • Minimalist abstract example

  • Applied examples

We organized the book in this manner to provide a well-rounded picture of each design pattern. By explaining the pattern and its key features, we focus on the pattern’s function and structure. The formal model and class diagram gives a wider overview, so you can see the structure and the interconnected classes and interface. We also included certain key OOP concepts for the different patterns. We did this for two reasons. First, the intermediate user will be better able to understand the OOP concept at work, and so understand OOP better. Second, we hoped that advanced users could see the concepts as shorthand to quickly see how the design pattern is structured.

What You Need to Use This Book

You will need either Flash CS3 or Flex 2 to work with the program examples in this book. All the applications were developed in the Flash IDE, so Flex 2 developers will need to make modifications, especially where certain features were developed using Flash drawing tools and components.

A few examples use Flash Media Server 2 (FMS2). The examples using FMS2 can be created using the Developer’s version of FMS2 and can be freely downloaded at http://www.adobe.com/downloads/. You will need either a Windows or Linux OS to run Flash Media Server 2. If you have a “MacTel” Macintosh, you can use the Window OS to run FMS, but if you have an older Macintosh running on the Motorola CPU, you’ll need to have a LAN or Internet access to a Windows or Linux platform running FMS2. Alternatively, you can skip the examples with FMS2.

Say It Again, Sam

One thing we can guarantee is redundancy. We know that people have different styles of learning. Some are conceptual learners, some experiential, and some metaphorical or any combinations of those, plus others we haven’t heard about yet. So you will see that we use several different ways to say the same thing with the idea that if you don’t get it one way, you’ll get it another.

At the same time, we feel that by discussing the same idea or concept in different ways and in different contexts, the specific sense of that concept is better shaped. In looking over reference materials published in books, articles, and online regarding Design Patterns, we found that some materials were not quite accurate in depicting some features. We worked very hard not to make mistakes, and so by providing numerous contexts, we can help filter out what we specifically mean, and, equally important, what we don’t mean. The ultimate authority is always Design Patterns: Elements of Reusable Object-Oriented Software, and if you have any questions about exactly what we mean, you can always check it out at the original source.

Over the years, a number of articles, books, dissertations and other treatises have appeared offering suggestions for improving the original design patterns. Some of these documents are quite useful, and even have the endorsement of members of the Gang of Four (GoF)— Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. Others are not too useful, especially for learning design patterns, and tend to complicate an already complex subject. As a result, we have not strayed from the path laid down in the original text by GoF.

User’s Guide

This book, at its core, is an introduction to a relatively advanced topic for writing reusable OOP code for ActionScript 3.0. Like “jumbo shrimp,” an “elementary introduction to an advanced topic” is an oxymoron. Advanced developers may want less of the elementary and less senior developers may demand more preparatory materials.

Because we cannot measure any reader’s level, we urge you to go through the Table of Contents and flip through the chapters to find what you want. Find your level and use the book at that level. For some, it will be an exercise of reading from cover to cover, while for others it will be a reference work for looking up how ActionScript 3.0 works with different features of design patterns. After all, it’s your book, and you should use it to best suit your needs.

Flex 2 developers

We developed all the examples for this book using Flash CS3. So, if you’re looking to use these examples for Flex 2, you’ll need to make the appropriate Flex adjustments. In some cases, examples were developed using user-created movie clip classes in the Flash CS3 IDE and stored in the Library panel. These cannot be employed directly using Flex 2. So, if you’re using Flex 2, plan on some workarounds.

Flash Media Server 2 developers

We have a few examples that use Flash Media Server 2 (FMS2). The examples don’t require anything more than the Developers Version of FMS2. The Developers Version can be freely downloaded from the Adobe site at http://www.adobe.com/devnet/flashmediaserver/. Alternatively, you can skip the examples, or substitute some other open socket technology.

Companion Tools You’ll Want

In an ideal world, those reading this book would have a solid background in object oriented programming and ActionScript 3.0. However, ActionScript 3.0 was released in a non-beta format in Flex 2 only about six months before this book, and in Flash at about the same time this book was published. So, you may not be familiar with ActionScript 3.0, and this book is not a tutorial in ActionScript 3.0. At a minimum, you will want to keep the ActionScript 3.0 Reference Guide handy along with any other ActionScript 3.0 documentation that comes with Flash CS3.

We strongly urge you to get a copy of Design Patterns: Elements of Reusable Object-Oriented Software sooner or later. At a minimum, check it out of your library. Another book we found to be invaluable is the wonderfully fun and enlightening book, Head First Design Patterns by Eric and Elisabeth Freeman (O’Reilly, 2004). All the examples are in Java, but even so, you’ll learn a great deal about design patterns and OOP. (Working out how to convert the Java examples to ActionScript 3.0 will teach you a lot about ActionScript 3.0 as well—however, before tackling the Java translations. be sure to go over the design pattern examples in this book.)

If you don’t have a good ActionScript 3.0 book yet, get one. We can recommend ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, and Keith Peters (O’Reilly, 2006) and Essential ActionScript 3.0 by Colin Moock (O’Reilly, 2007). For a very brief introduction to ActionScript 3.0, take a look at the Short Cut publication, ActionScript 3.0 Programming: Overview, Getting Started, and Examples of New Concepts by Bill Sanders (O’Reilly, 2007). Also, you’ll want an ActionScript 3.0 book on object-oriented programming. We’ve included an introduction to OOP in Chapter 1 that will suffice for now, and both the Moock and Lott books have some great OOP materials as well. However, a book dedicated to OOP concepts should be part of your library if you plan to write programs on the level of design patterns.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Indicates menu titles, menu options, menu buttons, and keyboard accelerators (such as Alt and Ctrl).

Bold

Indicates text that should be entered by the user.

Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, directories, and Unix utilities.

Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, types, classes, namespaces, methods, modules, properties, parameters, values, objects, events, event handlers, XML tags, HTML tags, , or the output from commands.

Constant width bold

Shows commands that should be typed literally by the user.

Constant width italic

Shows text that should be replaced with user-supplied values.

Note

This icon signifies a tip, suggestion, or general note.

Warning

This icon indicates a warning or caution.

Using Code Examples

This book will help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "ActionScript 3.0 Design Patterns, by Bill Sanders and Chandima Cumaranatunge. Copyright 2007 O’Reilly Media, Inc., ISBN978-0-596-52846-1.”

If you feel your use of code examples falls outside fair use or the permission given, feel free to contact us at .

We’d Like to Hear from You

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800 998-9938 (in the United States or Canada)
707 829-0515 (international or local)
707 829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/978-0-596-52846-1

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:

http://www.oreilly.com

Acknowledgments

Since being introduced to design patterns by aYo Binitie, these programming structures have been a topic of close interest. Like a lot of ActionScript developers, we are grateful to Colin Moock for breaching the topic of ActionScript’s use with design patterns, in Essential ActionScript 2.0. We are also grateful to Eric Freeman and Elisabeth Freeman for their fabulous Head First Design Patterns—even struggling through the Java code, we found it possible to appreciate how design patterns could be used in ActionScript 3.0. At the root of design patterns, we must acknowledge the venerable Gang of Four who produced Design Patterns: Elements of Reusable Object-Oriented Software, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides. We have spent endless hours poring over this tome.

Several people at Adobe were very generous with their time in helping out with the ActionScript 3.0 and some insights into design patterns. They include Chris Nuuja, Erica Norton, Geoffrey Williams, Grant Skinner, Jeffrey Mott, Mike Downey, Nivesh Rajbhandari, Peter DeHaan, Robert Penner, Gary Grossman, Ali Mills, Francis Cheng, David Mendels, Gordon Smith, Roger Gonzalez, Sho Kuwamoto, Francis Chen, Emmy Huang, Werner Sharp, Joan Tan, Phil Costa, Mally Gardiner, Asa Whillock, Chris Hock, Tareq Aljaber, San Khong, and Peter von dem Hagen.

In the Flash community, several Flash developers added further insight to both design patterns and ActionScript 3.0. They include Peter Hall, Aral Balkan, Robert Penner, Beau Ambur, Stefan Richter, Joey Lott, Guy Watson, Keith Peters, Will Law, and Brian Lesser. Jonathan Kaye, who brought state machines to ActionScript, was a huge help by going over a state design pattern that served as a model for what was developed for the book.

We’re also very grateful to Margot Maley Hutchison at Waterside Productions for helping to make the arrangements with O’Reilly Publishers. As always, Margot smoothed a complex process.

We are grateful to Professor John Gray, chair of the Multimedia Web Design and Development program at the University of Hartford. His encouragement and support in all matters pertaining to Internet and Web development, research and learning provide us with a rich atmosphere and wonderful resources to keep on track with the ever expanding universe of the technology we use.

Technical Reviewers

The technical reviewers had their job cut out for them. Some of the reviewers were experts on design patterns with C# and Java backgrounds, but were unfamiliar with ActionScript. Fortunately, ActionScript 3.0 is looking and acting like other OOP languages and so they were able to give us a great deal of help. Chief among this group was Adrienne Decker who is a Lecturer in the Department of Computer Science and Engineering at SUNY Buffalo. After sharing a session with Adrienne at the 2006 Object-Oriented Programming, Systems, Languages and Applications (OOPSLA) Conference in Portland, Oregon, it became clear that Adrienne not only was wise in the ways of design patterns, she was also very interested in how to best communicate the inner workings of design patterns. She was an immense help. Two other academics recruited to this project were Dr. James Heilotis and Dr. Axel Schreiner, both of the Rochester Institute of Technology, Department of Computer Science. Professor Schreiner had presented a design pattern named the Symmetric Proxy Pattern at the 2006 OOPSLA conference. Based on a paper, A Pattern for Distributing Turn-Based Games, by Heilotis and Schreiner, this new pattern seemed to be an excellent tool for gaming over the Internet using Flash and Flash Media Server 2. We believed that given the newness and creativity of the Symmetric Proxy Pattern, we would be well-advised to ask them for a technical review of our implementation and explanation of their pattern. They were very helpful in seeing to it that we were able to communicate their ideas, and we are most grateful to them both.

We were most fortunate to get the services of Todd Anderson. Todd is a bright light in the ActionScript 3.0 and design pattern mix. He helped us immensely and we are most grateful to Todd’s sharp eye and spot-on comments. Darren Richardson was our technical reviewer from the outset. He also brought an international perspective in case we got a bit too ethnocentric.

We also had the unique opportunity of working with The City’s Flash Coders New York (FCNY) group led by Jean-Charles Carelli. With considerable talent to spare, members of this group worked on (and over) our manuscript as technical reviewers. Their comments were quite helpful, and reflected an excellent cross-section of savvy ActionScript developers. Chief among those who aided in this process were Tyler Larson, Jim Kremens, Dominic Tancredi, Shari Halter, James O’Reilly, Andrew Hunt, Brian Weisenthal, Oscar Trelles, Seth Hillinger, Lisa Larson, and Edward Skrypa.

Editors

Our development editor, Robyn Thomas, had to make sense out of a highly technical corpus of work in addition to guiding us through The King’s English to insure what we said was what we meant. She also kept an array of technical editors supplied with chapters, updated where all the pieces were (and where they should be), and generally made the writing experience one of the nicest possible. Steve Weiss is an incredibly supportive publisher—getting everything organized and on track. Steve is also open to new perspectives and all things creative; most likely because he is so creative himself.

Authors

Bill Sanders, PhD, and Chandima Cumaranatunge, PhD, are both professors in the Multimedia Web Design and Development (MWD2) program at the University of Hartford. Bill teaches courses in Flash, ActionScript, Flash Media Server 2, PHP, C#, SQL, CSS and XHTML among other Internet languages. He has published 44 computer and computer-related books, written software ranging from Basic to Flash Media Server ActionScript, and served as a consultant for different computer software companies. Chandima teaches an introduction to the MWDD major, covering Flash and some ActionScript, a gaming course using Flash and ActionScript, as well as educational technology courses in the Education, Nursing, and Health Professions College. Recently he received a grant to teach an experimental course in robotics.

Bill Sanders

Bill would like to thank his co-author Chandima for a great writing experience and someone to really talk with about design patterns. Bill would also like to thank his wife Delia for her forbearance while she completed her doctorate and our two offices sang the song of two fully employed word processors. Our obsessive-compulsive Greater Swiss Mountain Dog, WillDe, added a measure of reality to both of our efforts. He always knew what was really important—going for a walk.

Chandima Cumaranatunge

Chandima considers himself very lucky, not only to have Bill as a writing partner, but as a professional mentor and friend. He is also eternally grateful to his wife Reshmaal for being totally supportive and putting up with his long writing stints away from the family. Finally, Chandima’s daughter Sayuri, the two-year-old “little lily” in “the ocean” (based on the meaning of her name in Japanese and Sinhalese) kept him sane by reminding him every day about the important things in life.

Dedication:

Bill would like to dedicate this to the new kids on the block, Ricky and Olivia.

Chandima would like to dedicate this book to his parents, Gaya and Lakshmie.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required