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

Abstraction

In general, an abstraction is a model or ideal. You don't have all of the details, but you have the general parameters that can be filled in with details. Further, an abstraction is clear enough for you to tell one abstraction from another. Take, for example, two jobs your company is trying to fill. One's for a Web designer and the other's for a programmer. To advertise for the position, you would not describe the person as a specific person but instead in terms of the characteristics you want for the position. You might have the two abstractions representing the two different positions:

Two Positions Open:

  • Programmer

  • Experienced with multi-programmer projects

  • Experienced with middleware and database programming

  • ECMAScript programming background

  • OOP and Design Pattern programming skills

  • Web designer

  • Experienced with creating Web graphics

  • Familiar with animation graphics

  • Can work with vector graphics

  • Client-centered approach

You can tell the difference between the two positions and their general requirements (properties), but the details are left fairly open. A programmer is unlikely to apply for the Web designer position and a designer is just as unlikely to apply for the programmer position. However, a pool of applicants could have a wide range of skills that would provide the concrete details for each position. For example, one programmer may have PHP middleware skills and/or MySQL database skills, while another may be experienced in using ASP.NET, C# and MS SQL. The abstraction is in the job description and the details are provided by the applicants' unique sets of skills and experience.

In Object-Oriented Design with Applications (Benjamin/Cummings), Grady Booch, one of the design pattern pioneers, provides the following definition of an abstraction that is both clear and succinct:

An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.

Booch's definition pretty well describes the two job descriptions. The descriptions provide the essential characteristics of the position and they distinguish one from the other.

Abstractions in ActionScript 3.0

Turning now to abstractions in ActionScript programming, we'll take a simple video player for an example. This player will be made of certain elements that we need; so we start by listing them as abstractions:

  • A Net connection

  • A video screen

  • A stream

  • An FLV file to play

If we put these together just right, we'll be able to play a video. However, instead of starting with an abstraction, we want to start with something concrete that works for us right away. Enter the code in Example 1-2 saving the file using the name in the caption:

Note

Throughout the book, with a few exceptions, caption names represent the name used for the file.

Example 1-2. PlayVideo.as

package
{
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.Video;
    import flash.display.Sprite;

    public class PlayVideo extends Sprite
    {
        public function PlayVideo()
        {
            var nc:NetConnection=new NetConnection();
            nc.connect(null);
            var ns:NetStream = new NetStream(nc);
            var vid:Video=new Video();
            vid.attachNetStream(ns);
            ns.play("adp.flv");
            addChild(vid);
            vid.x=100;
            vid.y=50;
        }
    }
}

You'll need an FLV file named adp.flv—any FLV file with that name will work. Open a new Flash document file, enter PlayVideo in the Document class window, and test it.

To change this to an abstract file, take out all specific references to any values with the exception of the null value in the NetConnection.connect() method. (We could pass that value as a string, but we're leaving it to keep things simple.) Example 1-3 shows essentially the same application abstracted to a "description" of what it requires to work.

Example 1-3. PlayVideoAbstract.as

package
{
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.Video;
    import flash.display.Sprite;

    public class PlayVideoAbstract extends Sprite
    {
     public function PlayVideoAbstract(nc:NetConnection,
           ns:NetStream,vid:Video,flick:String,xpos:uint,ypos:uint)
        {
            nc=new NetConnection();
            nc.connect(null);
            ns= new NetStream(nc);
            vid=new Video();
            vid.attachNetStream(ns);
            ns.play(flick);
            vid.x=xpos;
            vid.y=ypos;
            addChild(vid);
        }
    }
}

All the values for the different elements (with the exception of null) have been abstracted to describe the object. However, like a job description that abstracts requirements, so too does the PlayVideoAbstract class. All the particulars have been placed into one long set of parameters:

PlayVideoAbstract(nc:NetConnection,ns:NetStream,vid:Video,flick:String,
     xpos:uint,ypos:uint)

The abstract parameters in the constructor function let us add any concrete elements we want, including the specific name of a video we want to play. Example 1-4 shows how concrete instances are implemented from an abstract class:

Example 1-4. PlayAbstract.as

package
{
    import flash.display.Sprite
    import flash.net.NetConnection;
    import flash.net.NetStream;
    import flash.media.Video;

    public class PlayAbstract extends Sprite
    {
        private var conn:NetConnection;
        private var stream:NetStream;
        private var vid:Video;
        private var flick:String="adp.flv";
        public function PlayAbstract()
        {
var playIt:PlayVideoAbstract=new PlayVideoAbstract(conn,stream,vid,
    flick,100,50);
            addChild(playIt);
        }
    }
}

All the entire class does is to create a single instance of the PlayVideoAbstract class and place it on the stage. Private variables serve to provide most of the concrete values for the required parameters. Literals provide the data for both the horizontal (x) and vertical (y) positions of the video. To test it, just change the Document class name in the Flash document (FLA) file to PlayAbstract.

Why Abstractions Are Important

We can see two key reasons that abstractions are important for both OOP and Design Patterns. Rather than being dogged by minutiae of the problem, abstraction helps to focus on what parts, independent of their details, are required to solve the problem. Does this mean that you ignore the details? Not at all. Rather, the details are handled by adding them just when they're needed. For instance, in the example in the previous section, the exact video file is unimportant. All that's important is that some video name (a detail) be provided when we're ready to play it. We don't need to build a theater around a single movie. Likewise, we don't need to build a class around a single video file.

The second advantage of abstraction is flexibility. If you're thinking that in the previous section the Example 1-2 was easier and took less code and classes, you're right. However, suppose you want to place four videos on the stage. Then, all you would need to do is to create four instances using the abstract class instead of re-writing three more classes. In other words, the second method using abstraction is more flexible. In addition to adding more videos instances, we can easily change the video file we choose to play.

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