Cover image for ActionScript 3.0 Design Patterns

Book description

Now that ActionScript is reengineered from top to bottom as a true object-oriented programming (OOP) language, reusable design patterns are an ideal way to solve common problems in Flash and Flex applications. If you're an experienced Flash or Flex developer ready to tackle sophisticated programming techniques with ActionScript 3.0, this hands-on introduction to design patterns is the book you need. ActionScript 3.0 Design Patterns takes you step by step through the process, first by explaining how design patterns provide a clear road map for structuring code that actually makes OOP languages easier to learn and use. You then learn about various types of design patterns and construct small abstract examples before trying your hand at building full-fledged working applications outlined in the book. Topics in ActionScript 3.0 Design Patterns include:

  • Key features of ActionScript 3.0 and why it became an OOP language

  • OOP characteristics, such as classes, abstraction, inheritance, and polymorphism

  • The benefits of using design patterns

  • Creational patterns, including Factory and Singleton patterns

  • Structural patterns, including Decorator, Adapter, and Composite patterns

  • Behavioral patterns, including Command, Observer, Strategy, and State patterns

  • Multiple design patterns, including Model-View-Controller and Symmetric Proxy designs

During the course of the book, you'll work with examples of increasing complexity, such as an e-business application with service options that users can select, an interface for selecting a class of products and individual products in each class, an action game application, a video record and playback application, and many more. Whether you're coming to Flash and Flex from Java or C++, or have experience with ActionScript 2.0, ActionScript 3.0 Design Patterns will have you constructing truly elegant solutions for your Flash and Flex applications in no time.

Table of Contents

  1. ActionScript 3.0 Design Patterns
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Who this Book is for
      2. How this Book is Organized
      3. What You Need to Use This Book
        1. Say It Again, Sam
        2. User's Guide
          1. Flex 2 developers
          2. Flash Media Server 2 developers
      4. Companion Tools You'll Want
      5. Conventions Used in This Book
      6. Using Code Examples
      7. We'd Like to Hear from You
      8. Acknowledgments
      9. Technical Reviewers
      10. Editors
      11. Authors
        1. Bill Sanders
        2. Chandima Cumaranatunge
      12. Dedication:
    4. 1. Object-Oriented Programming, Design Patterns, and ActionScript 3.0
      1. The Pleasure of Doing Something Well
        1. Sequential and Procedural Programming
        2. Transition to OOP
          1. MovieClip and Button scripts
          2. Timeline scripts
          3. Document class
          4. Movie clip and button classes
      2. OOP Basics
      3. Abstraction
        1. Abstractions in ActionScript 3.0
        2. Why Abstractions Are Important
      4. Encapsulation
        1. Hiding Your Data from Bad Consequences
          1. Private variables
          2. The many meanings of interface
          3. Getters and setters
          4. The get and set methods
        2. Using Encapsulation and Design Patterns
      5. Inheritance
        1. Looking at the Ancestors
        2. Writing Classes for Inheritance
        3. Using Interfaces and Abstract Classes in ActionScript 3.0
          1. Interface constructs
          2. Abstract classes and overriding inheritance
          3. Why use interfaces and abstract classes?
      6. Polymorphism
        1. Generating Polymorphism Using An Abstract Class
        2. Implementing Polymorphism with Interfaces
      7. Principles of Design Pattern Development
        1. Implementation
        2. State
        3. Client and Request
      8. Program to Interfaces over Implementations
        1. Managing Dependency
        2. Using Complex Interfaces
      9. Favor Composition
        1. Doing Composition
        2. Using Delegation
        3. Making Composition, Inheritance, and Instantiation Work Together
      10. Maintenance and Extensibility Planning
        1. Planning Only for Maintenance
        2. Adding Extensibility to a Plan with Granularity
      11. Your Application Plan: It Ain't You Babe
        1. Using OOP and Design Patterns to Meet Client Goals
        2. Choosing the Right Design Pattern
        3. Achieving Better OOP Through Design Patterns
    5. 2. Factory Method Pattern
      1. What is the Factory Method Pattern?
        1. Model of the Factory Method Pattern
      2. Abstract Classes in ActionScript 3.0
      3. Minimalist Example
        1. Product Classes
        2. Creator Classes
        3. Clients
      4. Hiding the Product Classes
      5. Example: Print Shop
        1. Product Classes: Print Jobs
        2. Creator Classes: Print Centers
        3. Clients
        4. Print Shop Extension
        5. Parameterized Factory Methods
      6. Extended Example: Color Printing
        1. New Product Classes
        2. New Creator Classes: Integrating a Parameterized Factory Method
        3. Clients
        4. Parallel Class Hierarchies
      7. Key OOP Concepts Used in the Factory Method Pattern
      8. Example: Sprite Factory
        1. Product Classes: Shape Widgets
        2. Creator Classes: Shape Creators
        3. Clients
      9. Example: Vertical Shooter Game
        1. Product Classes
          1. Projectiles
          2. Space ships
        2. Creator Classes
          1. Weapon
          2. ShipCreator
        3. Concrete Creator Classes
        4. Clients
      10. Summary
    6. 3. Singleton Pattern
      1. What is the Singleton Pattern?
        1. Key Features
        2. The Singleton Model
      2. Key OOP Concepts Used with the Singleton Pattern
        1. Creating and Using a Private Class Constructor
        2. Creating a Private Class Instance
      3. Minimalist Abstract Singleton
        1. Instantiation with a Private Class Parameter
        2. A Singleton Instantiation Method
        3. One Instance and Different References
      4. When to Use the Singleton Pattern
        1. A Single Alert Message
        2. Just One Tune at a Time
        3. Using Multiple References in a Shopping Cart
      5. Summary
    7. 4. Decorator Pattern
      1. What is the Decorator Pattern?
        1. Key Features
        2. The Decorator Model
      2. Key OOP Concepts Used with the Decorator Pattern
        1. Unwanted Inheritance
        2. Wrapping Responsibilities
        3. Flexibility and Adaptability
      3. Minimalist Abstract Decorator
        1. Abstract Component Class
        2. Abstract Decorator Class
        3. Concrete Decorations
        4. Wrapping Up
      4. Applying a Simple Decorator Pattern in Flash: Paper Doll
        1. Setting Up the Component Class
        2. Decorator Class Dressing the Dolls
        3. The Concrete Classes
          1. Concrete component class
          2. Concrete decorator classes
        4. Implementing the Paper Doll Decorator
      5. Decorating with Deadly Sins and Heavenly Virtues
        1. Adding Properties and Methods
        2. Multiple Concrete Components
        3. Decorating with Multiple Properties
        4. Multiple Method Concrete Decorations
        5. The Good and Evil Concrete Decorators
          1. Heavenly Virtues
          2. Deadly Sins
        6. Implementing the Good and Evil Decorator
          1. Dual implementation
          2. Charting souls
      6. Dynamic Selection of Concrete Components and Decorations: a Hybrid Car Dealership
        1. Setting Up the Hybrid Car Components
          1. Auto abstract component
          2. Hybrid car classes concrete component
        2. Using Auto Options as Decorators
          1. The options abstract decorator
          2. The options concrete decorators
        3. Setting Up the User Interface
          1. Creating the document and setting the stage
          2. Implementing the concrete components and their decorators
      7. Summary
    8. 5. Adapter Pattern
      1. What is the Adapter Pattern?
        1. A Design Pattern for Potty Training
        2. Key Features of the Adapter Pattern
      2. Object and Class Adapters
        1. Object Adapters
          1. Minimalist example of an object adapter
          2. Using a parameterized adapter class
        2. Class Adapters
          1. Minimalist example of a class adapter
      3. Key OOP Concepts in the Adapter Pattern
        1. Object and Class Adapters Compared
      4. Example: Car Steering Adapter
        1. The Existing Class
        2. Interface Conversion
          1. The original interface
          2. The new context
          3. The new interface
        3. The Adapter Class
        4. The Client
      5. Extended Example: Steering the Car Using a Mouse
      6. Example: List Display Adapter
        1. The Existing Class
        2. Interface Conversion
        3. The Adapter Class
        4. The Client
      7. Extended Example: Displaying the O'Reilly New Books List
      8. Summary
    9. 6. Composite Pattern
      1. What is the Composite Pattern?
        1. Key Features of the Composite Pattern
      2. Minimalist Example of a Composite Pattern
        1. Accessing Child Nodes
        2. Removing Nodes
          1. Creating a parent reference
          2. Implementing the remove method
        3. Building and Manipulating a Composite Structure
      3. Key OOP Concepts in the Composite Pattern
      4. Example: Music Playlists
        1. Queuing Songs to Play in Sequence
        2. Building Composite Playlists
      5. Example: Animating Composite Objects Using Inverse Kinematics
        1. Using Inverse Kinematics
        2. Creating Component and Composite Nodes for the Snake
        3. Building the Snake Head
        4. Controlling the Snake
        5. Moving a Kinematic Pair
        6. Building the Body and Tail Segments
        7. Building the Composite Snake
      6. Using Flash's Built-in Composite Structure: the Display List
        1. Creating a Composite Airplane
        2. Developing the Component and Composite Classes for the Airplane
        3. Creating the Fuselage, Wings, and Engines
        4. Building the Composite Structure
        5. Calculating the Total Weight of the Airplane
        6. Modifying Components to Reflect Damage
        7. Calculating Total Damage to the Airplane
      7. Summary
    10. 7. Command Pattern
      1. What is the Command Pattern?
        1. Mom Needs to Issue Some Commands
        2. Key Features of the Command Pattern
        3. Class Diagram of the Command Pattern
      2. Minimalist Example of a Command Pattern
        1. The Command Interface
        2. The Concrete Command
        3. The Receiver
        4. The Invoker
        5. The Client
        6. Setting a Trigger to Invoke the Command
      3. Key OOP Concepts in the Command Pattern
      4. Minimalist Example: Macro Commands
        1. The Macro Command Interface
        2. Two Concrete Subcommands
        3. The Concrete Macro Command
        4. A Macro Command Object Created from the Client
      5. Example: Number Manipulator
        1. A Utility Button Class
        2. Triggering an Invoker by Button Clicks
        3. The Increment and Decrement Commands
        4. The Client
      6. Extended Example: Sharing Command Objects
        1. Triggering an Invoker by Key Presses
        2. Sharing Command Objects from the Client
      7. Extended Example: Implementing Undo
        1. An Abstract Interface for Commands
        2. Concrete Commands that Implement Undo
        3. Undoable Commands Assigned from the Client
      8. Example: Podcast Radio
        1. What is a Podcast?
        2. Creating a Package with Utility Classes
        3. Creating a Command to Play a Podcast
        4. Developing the Radio Receiver
        5. Push Button Invokers for the Radio
        6. The Client Assigns Podcasts to Push Buttons
      9. Extended Example: Dynamic Command Object Assignment
        1. A Context Sensitive Invoker
        2. Commands to Dynamically Assign Command Objects
        3. Dynamic Command Assignment Setup from the Client
      10. Summary
    11. 8. Observer Pattern
      1. What is the Observer Pattern?
        1. Key Features
        2. The Observer Model
      2. Key OOP Concepts Used with the Observer Pattern
        1. Choose the Interface
        2. Object Composition
      3. Minimalist Abstract Observer
        1. Subject Interface
        2. Observer Interface
        3. Concrete Subject
        4. Concrete Observer
        5. Working the Observer
      4. Example: Adding States and Identifying Users
        1. Multiple States
        2. Who are You?
        3. Updated Observer
        4. Playing the Bugle
      5. Dynamically Changing States
        1. Recording a Space Battle
        2. Giving More Work to the Concrete Classes
        3. Launching the Space Battle
          1. Setting up the Flash document
          2. Building the spaceships
          3. Building the weapons
          4. Building the Android space station and beam
          5. Writing a Change Handler
      6. Example: Working with Different Data Displays
        1. The Output Designer
        2. The Concrete Classes and a Double Implementation
        3. The Data Design Classes
          1. UIList component
          2. Bar chart display
          3. The line graph
        4. Pulling All the Elements Together
      7. Summary
    12. 9. Template Method Pattern
      1. What is the Template Method Pattern?
        1. Key Features
        2. The Template Method Model
      2. Key OOP Concepts used with the Template Method
        1. Why Inheritance and Not Composition?
        2. Abstract Functions and Override Flexibility
        3. Consider Variation
        4. The Hollywood Principle
      3. Minimalist Example: Abstract Template Method
        1. Bare Bones Template Method
        2. Testing Templates
      4. Employing Flexibility in the Template Method
      5. Selecting and Playing Sound and Video
        1. Setting Up the Format
        2. Not-So-Concrete Concrete Classes
        3. The Detail Classes
        4. Playing the Media
      6. Hooking It Up
        1. When to Hook?
        2. The Hook as a Lonely Repairman
        3. Baja Flight with a Hook
      7. Summary
    13. 10. State Design Pattern
      1. Design Pattern to Create a State Machine
        1. Key Features
        2. The State Model
          1. Transitions
          2. Triggers
          3. State design structure
      2. Key OOP Concepts Used with the State Pattern
      3. Minimalist Abstract State Pattern
        1. Managing All Those States: Hardworking Context Class
          1. Creating a context class
          2. Completing and testing the abstract state machine
      4. Video Player Concrete State Application
      5. Expanding the State Design: Adding States
        1. Adding the Pause State to the Statechart
        2. Adding New Behaviors
      6. Adding More States and Streaming Capabilities
        1. Setting Up Your ActionScript 3.0 Script for FMS2
        2. The Adaptable States
      7. Summary
    14. 11. Strategy Pattern
      1. What is the Strategy Pattern?
        1. Key Features
        2. The Strategy Model
      2. Key OOP Concepts Used with the Strategy Pattern
        1. Encapsulating Variation
          1. Unencapsulated version (pseudocode)
          2. Encapsulated algorithms (pseudocode)
        2. Using Delegation and Delegates
      3. Minimalist Abstract State Pattern
        1. Using Delegation with the Context
        2. Adding a Strategy
        3. Details of the Strategy
        4. More Delegation in a Concrete Context
        5. Pulling All the Parts Together
      4. Adding More Concrete Strategies and Concrete Contexts
        1. The Clowns
        2. The Trick Interface and Implementations
        3. The Skits Interface and Implementations
        4. Here Come the Clowns!
        5. Additional Clown Functionality
          1. Adding a new clown
          2. Adding a new trick
          3. Revising clown college
        6. Tricks and Skits Reorganization: Clown Planning
      5. Working with String Strategies
        1. Contexts for String Strategies
        2. String Strategies
          1. Checking strategies
          2. Sort strategies
        3. Support Classes
        4. String Strategy Test
      6. Summary
    15. 12. Model-View-Controller Pattern
      1. What is the Model-View-Controller (MVC) Pattern?
        1. Model
        2. View
        3. Controller
      2. Communication Between the MVC elements
      3. Embedded Patterns in the MVC
      4. Minimalist Example of an MVC Pattern
        1. Model as a Concrete Subject in an Observer Pattern
          1. The EventDispatcher class in ActionScript 3.0
          2. The model
        2. Controller as a Concrete Strategy in a Strategy Pattern
          1. The controller
        3. View as a Concrete Observer in an Observer Pattern and context in a Strategy Pattern
        4. Building the MVC Triad
        5. Nested Views as Leaves and Nodes of a Composite Pattern
          1. Component and composite views
          2. Creating nested views
        6. Building the Nested View Structure
        7. Key Features of the MVC Pattern
      5. Key OOP Concepts in the MVC Pattern
      6. Example: Weather Maps
        1. The Model
        2. The Controller
        3. The Views
          1. Combo box view
          2. Map view
        4. Building the MVC Triad
        5. Setting the Model to Self-Update
      7. Extended Example: Infrared Weather Maps
        1. Adding a New Model
        2. Adding a New Controller
        3. Adding a New View
        4. Building the MVC Triad
      8. Example: Cars
        1. The Model
        2. The Controller
        3. The Views
        4. Building the Car
      9. Custom Views
        1. Direction Gauge View
        2. GPS View
        3. Adding the Custom Views
      10. Adding a Chase Car
      11. Summary
    16. 13. Symmetric Proxy Pattern
      1. Simultaneous Game Moves and Outcomes
        1. The Player
        2. The Referee
        3. The Internet, Proxies, and Players
      2. The Symmetric Proxy Pattern
        1. Key Features
          1. The cell
          2. The proxy
      3. Key OOP Concepts Used with the Symmetric Proxy
        1. Communication Flexibility
        2. Game Flexibility
      4. The Player Interface
      5. The Referee
        1. Methods
        2. Template Method
        3. RPS Subclass
      6. Information Shared over the Internet
      7. Player-Proxy Classes
        1. Move Making
          1. Event to move
          2. Dual moves
          3. Proxy move
          4. Referee object
        2. Player 2 Changes
      8. Classes and Document Files Support
        1. Dynamic Output Text Fields
        2. Button Controls
        3. The Flash File and Connection Movie Clip
      9. Summary
    17. About the Authors
    18. SPECIAL OFFER: Upgrade this ebook with O’Reilly