Class Design Pattern

A Class in ActionScript is the "recipe" for creating an object. An object is a runtime container of Data and Actions (aka, Methods or Operations or Functions). A single class can be "instantiated" multiple times, to create many runtime objects which all follow the same "blue print".

ActionScript Design Pattern for a Class

Below is a design pattern for an ActionScript class. You should commit all the listed sections to memory and be able to create a class file from Memory without this design pattern, but initially it is of great benefit to be able to refer to this DP each time you create a class.

See below the code for a description of each section.

	

/**
 *  File:    file.as
 *
 *  Author:  
 *  Date:
 *  Course:
 *  Partner: 
 * 
 *  Description:
 * 
 *      List the purpose and use of the class
 * 
 *  
 * 
 */

package   // 1: PACKAGE 
{
        import flash.display.Sprite;  // 2: IMPORT STATEMENTS

        // 3: CLASS DEFINTION
        public class Class_Design_Pattern  // Optional: extends Class 
        {
                
        /**
         *  4: OBJECT LEVEL VARIABLES
         *
         * List all the object level variables and their purposes.
         * These variables can be used in any method below, and should be
         * initialized either here or in the constructor
         */

         public  var object_variable_1 : TYPE;
         public  var object_variable_2 : TYPE;
         private var object_variable_3 : Number  = 50;

        /**
         * Constructor for _Class_Name_
         *
         *    - Remember: The constructor initializes all values - (delete me)
         *   
         * Description  : The base state of the object is ...
         *
         */
        public function 
        Class_Design_Pattern()
        {
        
        }

        /**
         *
         * Summary of the Get_X function:
         *
         *    Return the value of field X
         *
         * Parameters   : None
         * Return Value : The value of X, ... any special qualifications ...
         *
         */
        public function
        get_value_1(  ) : RETURN_TYPE 
        {
                return object_variable_1;
        }

        /**
         *
         * Summary of the Set_X function:
         *
         *    Set the value of field X
         *
         * Parameters   : data to place in field X
         * Return Value : None
         *
         */
        public function
        set_value_1( data : TYPE ) : void
        {
                object_variable_1 = data;
        }

        /**
         *
         * Summary of the do_something_internal function:
         *
         *    This is a helper function used to do ...
         *
         * Parameters   : None ( or it could take in something )
         * Return Value : None ( or it could return something )
         *
         */
        private function
        do_something_internal(  ) : void
        {       
           // only let other methods inside the class use this method
           // this is often called a "helper" method
        }
        
        /**
         *
         * Summary of the do_something_external function:
         *
         *    This function does X to the object
         *
         * Parameters   : None ( or it could take in something )
         * Return Value : None ( or it could return something )
         *
         */
        public function
        do_something_external(  ) : void
        {       
           // being public lets code outside of the
           // class invoke this method
        }

        /**
         *
         * Summary of the toString function:
         *
         *    This function returns a string representation of
         *    the object...
         *
         * Parameters   : None
         * Return Value : String - a concise representation of the object
         *
         */
         public override function
         toString() : String
         {
                return "A concise description of the state of the object";
         }

        /**
         *
         * Summary of the create_display_list function:
         *
         *    This function builds the "drawing" of the object...
         *
         * Parameters   : None ( or something )
         * Return Value : none
         *
         */
         public function
         create_display_list() : void
         {
                // draw something
         }

}
        
      

Required and Suggested Sections of a Class

Below are comments about each of the numbered sections listed above in the class design pattern. In general each method in the design pattern should be commented, as should the entire class in the "header" comment.

  1. The Package

    A package is a "name-space" for keeping your code separate from other peoples. This is useful when you are using names that other people might use. For now we are using the "default" name-space (or i.e., the unnamed package).

  2. Import Statements

    Every "outside" class that you use to help define your new object class must be "requested" from Flash by using the import statement. For example, if you are going to "extend Sprite" you must import the Sprite class to tell Flash which Sprite you mean.

  3. Class Definition

    Every class is defined by "public class NAME" where NAME should be a symbolic name describing what the class is representing. Additionally, if the class you are writing, is based on another class, you must use the extends keyword.

    All our classes will be public, which means, any program can use the class.

  4. Object Level Variables

    Write the name and type of every variable that is necessary to "encompass" what it means to be this object. These object level variables can be used in any method listed below and serve as the "memory" of the class.

    Please note, when you have multiple "objects" created from this class file, each object will contain its own version of each variable, thus we could have 10 "star" objects, and every one of them would have their own X,Y location in the sky.

    Public vs. Private

    • Public

      Public variables are available to (and thus can be modified by) any other code. This can often lead to subtle errors where an outside piece of code makes a change that the "inside" code doesn't expect. It is thus often a good idea to make all the variables private and to create accessors (get and set) methods for each one (see below).

    • Private

      Private variables are only accessible by the methods inside the class, and thus are more likely to remain in a "correct" state.

  5. The Constructor

    A Constructor is a function that is invoked (called) as soon as the object is created. The construct must be named the exact same name as the Class.

    The purpose of the constructor is to initialize starting values for every object level variable.

    Warning: If you put :void (a return value) after the constructor name, things will go wrong.

    Every class should have a constructor if it needs any kind of "setup". The alternative to a constructor, for simple classes, is to assign a "default" value to each object level variable. (see object_variable_3 in the design pattern).

  6. Get Methods

    As stated above, it is "risky" to allow any code outside of a class to have direct access to the class variables. Thus making most class variables "private" is often a good idea. The problem then becomes, how can anyone outside of the class "see what is going on" inside the class. The answer is "get_methods".

    Get methods provide a "window" into the object without allowing "outside influences" to modify the object. Get methods almost always go along with set methods (see below).

  7. Set Methods

    Again, allowing outside code to directly manipulate (set) the value of object variables can be risky. For example, what if the object represents a fraction, and someone sets the divisor to 0, causing a division by 0 error.

    To safe guard the "State" of an object, we often use "set" methods which usually test to make sure the value being assigned to the object level variable is a valid one. If the value is invalid, several options exist, including throwing an error, printing an error message and using a default value, stopping the program, etc.

                
            
            /**
             * set_divisor
             * 
             * This method sets the divisor value of a fraction.  The divisor
             * must be non zero.  If a zero is passed in, an error will be printed
             * and the value 1 used instead.
             */
            public function
            set_divisor( new_divisor : int ) : void
            {
                 if (new_divisor != 0)
                   {
                     this.divisor = new_divisor;
                   }
            }
                
              
  8. Private or "Helper" Methods

    Often a class will have actions that are quite complex. Rather than put all the code into one function, the action can be broken up into a few smaller (and hopefully simpler) actions. These actions are often only used internally to the class. To make sure this is the case, they are labeled "private".

  9. Public Actions

    The public actions of a class are what, for the most part, define what a class is (what it does). These functions are the INTERFACE between the internal workings of the class and the world.

    Such function use the keyword "public" in front of the keyword "function".

  10. toString

    See toString function

  11. create_display_list

    This method is only used for graphical objects (those you want to see on the "stage").

    The create_display_list function should be used along with the "this.graphics" sub object to define how to display an object on the screen. The create_display_list function should either be called by the class constructor, or later on by an external piece of code after the object has been "addChild"ed to the display (this is useful when you want the "stage" property to be correctly set inside of the object).

    By putting all the display list creation code into a single place, it make writing, maintaining, updating, and debugging much easier.