Transcription of ActionScript 3.0 Design Patterns - Adobe
1 ActionScript Design Patterns William B. Sanders andChandimaCumaranatungeBeijing Cambridge Farnham K ln Paris Sebastopol Taipei Tokyo, Page iii Tuesday, July 3, 2007 2:53 PMActionScript Design Patternsby William B. Sanders and Chandima CumaranatungeCopyright 2007 O Reilly Media, Inc. All rights in the United States of by O Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA Reilly books may be purchased for educational, business, or sales promotional use. Online editionsare also available for most titles ( ). For more information, contact ourcorporate/institutional sales department: (800) 998-9938 WeissDevelopmental Editor:Robyn G. ThomasProduction Editor:Philip DanglerCopyeditor:Sohaila AbdulaliIndexer:John BickelhauptCover Designer:Karen MontgomeryInterior Designer:David FutatoIllustrators:Robert Romano and Jessamyn ReadPrinting History:July 20007:First Handbook, the Nutshell Handbook logo, and the O Reilly logo are registered trademarks ofO Reilly Media, Inc.
2 ActionScript Design Patterns , the image of a rosy feather starfish, and relatedtrade dress are trademarks of O Reilly Media, of the designations used by manufacturers and sellers to distinguish their products are claimed astrademarks. Where those designations appear in this book, and O Reilly Media, Inc. was aware of atrademark claim, the designations have been printed in caps or initial every precaution has been taken in the preparation of this book, the publisher and authorsassume no responsibility for errors or omissions, or for damages resulting from the use of theinformation contained book uses RepKover , a durable and flexible lay-flat : 0-596-52846-9 ISBN-13: 978-0-59652846-1[M], Page iv Tuesday, July 3, 2007 2:53 PMAdobe Developer Library, a copublishing partnership between O Reilly Media Inc.
3 ,and Adobe Systems, Inc., is the authoritative resource for developers using Adobetechnologies. These comprehensive resources offer learning solutions to help devel-opers create cutting-edge interactive web applications that can reach virtually any-one on any top-quality books and innovative online resources covering the latest tools forrich-Internet application development, theAdobe Developer Librarydelivers experttraining straight from the source. Topics include ActionScript , Adobe Flex , AdobeFlash , and Adobe Acrobat .Get the latest news about books, online resources, and more This excerpt is protected by copyright law. It is your responsibility to obtain permissions necessary for any proposed use of this material. Please direct your inquiries to viiTable of ContentsPreface.
4 XiPart Programming, Design Patterns , and ActionScript ..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 Method Pattern ..65 What Is the Factory Method Pattern? 65 Abstract Classes in ActionScript 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, Page vii Tuesday, July 3, 2007 2:53 PMviii|Table of ContentsExample: Sprite Factory 84 Example: Vertical Shooter Game 90 Summary Pattern.
5 101 What Is the Singleton Pattern? 101 Key OOP Concepts Used with the Singleton Pattern 102 Minimalist Abstract Singleton 105 When to Use the Singleton Pattern 112 Summary 125 Part Pattern ..129 What Is the Decorator Pattern? 129 Key OOP Concepts Used with the Decorator Pattern 132 Minimalist Abstract Decorator 135 Applying a Simple Decorator Pattern in Flash: Paper Doll 141 Decorating with Deadly Sins and Heavenly Virtues 148 Dynamic Selection of Concrete Components and Decorations:A Hybrid Car Dealership 164 Summary Pattern ..177 What Is the Adapter Pattern? 177 Object and Class Adapters 179 Key OOP Concepts in the Adapter Pattern 185 Example: Car Steering Adapter 185 Extended Example: Steering the Car Using a Mouse 193 Example: List Display Adapter 194 Extended Example: Displaying the O Reilly New Books List 199 Summary Pattern.
6 204 What Is the Composite Pattern? 204 Minimalist Example of a Composite Pattern 207 Key OOP Concepts in the Composite Pattern 217 Example: Music Playlists 217 Example: Animating Composite Objects Using Inverse Kinematics 222, Page viii Tuesday, July 3, 2007 2:53 PMTable of Contents|ixUsing Flash s Built-in Composite Structure: the Display List 233 Summary 243 Part Pattern ..247 What Is the Command Pattern? 247 Minimalist Example of a Command Pattern 251 Key OOP Concepts in the Command Pattern 255 Minimalist Example: Macro Commands 255 Example: Number Manipulator 258 Extended Example: Sharing Command Objects 263 Extended Example: Implementing Undo 266 Example: Podcast Radio 270 Extended Example: Dynamic Command Object Assignment 276 Summary Pattern.
7 282 What Is the Observer Pattern? 282 Key OOP Concepts Used with the Observer Pattern 285 Minimalist Abstract Observer 289 Example: Adding States and Identifying Users 294 Dynamically Changing States 302 Example: Working with Different Data Displays 318 Summary Method Pattern ..331 What Is the Template Method Pattern? 331 Key OOP Concepts Used with the Template Method 335 Minimalist Example: Abstract Template Method 338 Employing Flexibility in the Template Method 341 Selecting and Playing Sound and Video 344 Hooking It Up 351 Summary Pattern..357 Design Pattern to Create a State Machine 357 Key OOP Concepts Used with the State Pattern 360 Minimalist Abstract State Pattern 361, Page ix Tuesday, July 3, 2007 2:53 PMx|Table of ContentsVideo Player Concrete State Application 367 Expanding the State Design : Adding States 374 Adding More States and Streaming Capabilities 382 Summary Pattern.
8 398 What Is the Strategy Pattern? 398 Key OOP Concepts Used with the Strategy Pattern 400 Minimalist Abstract State Pattern 402 Adding More Concrete Strategies and Concrete Contexts 406 Working with String Strategies 414 Summary 423 Part Pattern..427 What Is the Model-View-Controller (MVC) Pattern? 427 Communication Between the MVC Elements 428 Embedded Patterns in the MVC 430 Minimalist Example of an MVC Pattern 431 Key OOP Concepts in the MVC Pattern 443 Example: Weather Maps 443 Extended Example: Infrared Weather Maps 451 Example: Cars 457 Custom Views 463 Adding a Chase Car 466 Summary Proxy Pattern ..469 Simultaneous Game Moves and Outcomes 469 The Symmetric Proxy Pattern 473 Key OOP Concepts Used with the Symmetric Proxy 475 The Player Interface 477 The Referee 478 Information Shared Over the Internet 483 Player-Proxy Classes 486 Classes and Document Files Support 494 Summary 498 Index.
9 499, Page x Tuesday, July 3, 2007 2:53 PM427 Chapter12 CHAPTER 12 Model-View-Controller Pattern12 According to the standard model billions of years agosome little quantum fluctuation, perhaps a slightlylower density of matter, maybe right where we resitting right now, caused our galaxy to start collapsingaround here. Seth LloydWe view things not only from different sides, but withdifferent eyes; we have no wish to find them alike. Blaise PascalThe primary symptom of a controller is denial, that isI can t see its symptoms in myself. Keith MillerWhat Is the Model-View-Controller (MVC) Pattern?The Model-View-Controller (MVC) is a compound pattern, or multiple patternsworking together to create complex applications. The MVC pattern is most com-monly used to create interfaces for software applications, and, as the name implies,consists of three the application data and logic to manage the state of the applicationViewPresents the user interface and the state of the application onscreenControllerHandles user input to change the state of the applicationThe power of the MVC pattern can be directly attributed to the separation of thethree elements without overlap in each of their responsibilities.
10 Let s look at each ele-ment s responsibilities., Page 427 Tuesday, July 3, 2007 2:54 PM428|Chapter 12: Model-View-Controller PatternModelThe model is responsible for managing the state of the application. The applicationlogic in the model performs two important tasks: it responds to requests for informa-tionaboutthestateoftheapplicatio n, view is the external face of the application. Users interact with the applicationthrough the view. An application can contain multiple views that can be both inputsand outputs. For example, in the case of a portable digital music player such as aniPod, the screen is a view. In addition, the buttons that control song playback areviews as well. The screen shows the name of the current song, song duration, albumart, and so on, that communicate the current state of the device.
