
,title.3638 Page iii Tuesday, July 3, 2007 2:53 PM ActionScript 3.0 Design Patterns William B. Sanders and Chandima Cumaranatunge Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo ,copyright.3756 Page iv 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 [email protected]. Editor: Steve Weiss Indexer: John Bickelhaupt Developmental Editor: Robyn G. Thomas Cover Designer: Karen Montgomery Production Editor: Philip Dangler Interior Designer: David Futato Copyeditor: Sohaila Abdulali 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] 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. 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 [email protected]. ,asdpTOC.fm.3994 Page vii Tuesday, July 3, 2007 2:53 PM 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 vii ,asdpTOC.fm.3994 Page viii Tuesday, July 3, 2007 2:53 PM Example: Sprite Factory 84 Example: Vertical Shooter Game 90 Summary 100 3. Singleton Pattern . 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 III. Structural Patterns 4. Decorator 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 176 5. Adapter 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 203 6. Composite Pattern . 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 viii | Table of Contents ,asdpTOC.fm.3994 Page ix Tuesday, July 3, 2007 2:53 PM Using Flash’s Built-in Composite Structure: the Display List 233 Summary 243 Part IV. Behavioral Patterns 7. Command 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 281 8. Observer Pattern . 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 330 9. Template 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 356 10. State Pattern . 357 Design Pattern to Create a State Machine 357 Key OOP Concepts Used with the State Pattern 360 Minimalist Abstract State Pattern 361 Table of Contents | ix ,asdpTOC.fm.3994 Page x Tuesday, July 3, 2007 2:53 PM Video Player Concrete State Application 367 Expanding the State Design: Adding States 374 Adding More States and Streaming Capabilities 382 Summary 397 11. Strategy Pattern . 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 V. Multiple Patterns 12. Model-View-Controller 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 468 13. Symmetric 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 . 499 x | Table of Contents ,ch12.5543 Page 427 Tuesday, July 3, 2007 2:54 PM Chapter 12 CHAPTER 12 Model-View-Controller Pattern12 According to the standard model billions of years ago some little quantum fluctuation, perhaps a slightly lower density of matter, maybe right where we’re sitting right now, caused our galaxy to start collapsing around here. —Seth Lloyd We view things not only from different sides, but with different eyes; we have no wish to find them alike. —Blaise Pascal The primary symptom of a controller is denial, that is I can’t see its symptoms in myself. —Keith Miller What Is the Model-View-Controller (MVC) Pattern? The Model-View-Controller (MVC) is a compound pattern, or multiple patterns working 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 elements. Model Contains the application data and logic to manage the state of the application View Presents the user interface and the state of the application onscreen Controller Handles user input to change the state of the application The power of the MVC pattern can be directly attributed to the separation of the three elements without overlap in each of their responsibilities. Let’s look at each ele- ment’s responsibilities. 427 ,ch12.5543 Page 428 Tuesday, July 3, 2007 2:54 PM Model The model is responsible for managing the state of the application. The application logic in the model performs two important tasks: it responds to requests for informa- tion about the state of the application, and takes action on requests to change the state. View A view is the external face of the application. Users interact with the application through the view. An application can contain multiple views that can be both inputs and outputs. For example, in the case of a portable digital music player such as an iPod, the screen is a view. In addition, the buttons that control song playback are views as well. The screen shows the name of the current song, song duration, album art, and so on, that communicate the current state of the device. Views don’t neces- sarily have to be visual. In the case of a digital music player, the sound that comes through the headphones represents a view as well. For example, clicking a button may provide some auditory feedback in the form of the click sound. Changing the volume is reflected in the audio output as well.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages51 Page
-
File Size-