AS 3 Config Class

April 2nd, 2008

As I move into more elaborate AS 3 development, I began looking for a way to templatize my config objects for each of my classes. This is my first attempt at an Abstract Config class.

package com.jessefreeman.configs {
        import flash.events.EventDispatcher;   
       
        /**
         * @author jessefreeman
         * Use this as a base class for any configuration objects. It has built in support for a few basic values but should be extended to meet the needs of your application.
         */

        public class AbstractConfig extends EventDispatcher {
               
                /** Some Default variables **/
                protected var _name : String;
                protected var _x : Number;
                protected var _y : Number;
                protected var _width : Number;
                protected var _height : Number;
                protected var _alpha : Number;
                protected var _rotation : Number;
                protected var _debug : Boolean;
               
                /**
                 * Get function for _name.
                 * @return _name : String
                 */

                public function get name () : String {
                        return _name;
                }
               
                /**
                 * Set function for _name.
                 * @param value : String
                 */

                public function set name (value : String):void {
                        _name = value;
                }
               
                /**
                 * Get function for _x.
                 * @return _x : Number
                 */

                public function get x () : Number {
                        return _x;
                }
               
                /**
                 * Set function for _x.
                 * @param value : Number
                 */

                public function set x (value : Number):void {
                        _x = value;
                }
               
                /**
                 * Get function for _y.
                 * @return _y : Number
                 */

                public function get y () : Number {
                        return _y;
                }
               
                /**
                 * Set function for _y.
                 * @param value : Number
                 */

                public function set y (value : Number):void {
                        _y = value;
                }
               
                /**
                 * Get function for _width.
                 * @return _width : Number
                 */

                public function get width () : Number {
                        return _width;
                }
               
                /**
                 * Set function for _width.
                 * @param value : Number
                 */

                public function set width (value : Number):void {
                        _width = value;
                }
               
                /**
                 * Get function for _height.
                 * @return _height : Number
                 */

                public function get height () : Number {
                        return _height;
                }
               
                /**
                 * Set function for _height.
                 * @param value : Number
                 */

                public function set height (value : Number):void {
                        _height = value;
                }
               
                /**
                 * Get function for _alpha.
                 * @return _alpha : Number
                 */

                public function get alpha () : Number {
                        return _alpha;
                }
               
                /**
                 * Set function for _alpha.
                 * @param value : Number
                 */

                public function set alpha (value : Number):void {
                        _alpha = value;
                }
               
                /**
                 * Get function for _rotation.
                 * @return _rotation : Number
                 */

                public function get rotation () : Number {
                        return _rotation;
                }
               
                /**
                 * Set function for _rotation.
                 * @param value : Number
                 */

                public function set rotation (value : Number):void {
                        _rotation = value;
                }
               
                /**
                 * Get function for _debug.
                 * @return _debug : Boolean
                 */

                public function get debug (): Boolean {
                        return _debug;
                }
               
                /**
                 * Set function for _debug.
                 * @param value : Boolean
                 */

                public function set debug (value: Boolean):void {
                        _debug = value;
                }
               
                /**
                 * Constructor
                 */

                public function AbstractConfig() {
                        //Add custom logic here if needed
                }
               
               
        }
}

So in the past I would do something like this:

// Config Object
var config:Object = new Object();
        config.name = “instance name”;
        config.x = 50;
        config.y = 150;
var new_class:NewClass = new NewClass(config);

In this setup I would use the config Object to organize all the settings for the class. I have been doing this for a while now but the biggest drawback comes in 2 situations:

  1. You can’t really type values attached dynamically to a generic object. Also, because of AS 3’s strict enforcement of sealed classes, this just seems so wrong.
  2. In team development environments its not always 100% clear how a class should be configured. With no way of really enforcing what kind of data goes into a class, there is additional room for error.

So I came up with the AbstractConfigClass to alleviate the above problems. By using the AbstractConfigClass as a base for any config object I need to make, I not only inherit some of the most reused values most of my classes share, but I get to add custom getters and setter to the extended config classes to handle each situation it is deployed in. There isn’t much of a change in how it is set up but, now I am using the config object and taking advantage of the compiler to let me know when I miss configure the class:

// CustomConfig Object that extends AbstractConfigClass
var config:NewClassConfig = new config:NewClassConfig();
        config.name = “instance name”;
        config.x = 50;
        config.y = 150;
var new_class:NewClass = new NewClass(config);

Now I am the first to admit this is a little overkill but, when you work in a multi-developer environment, or you share your code with lots of people, you can add checks and balances to how your classes get implemented and configured. Also, the greatest advantage of using this class is in your ability to set up default values in the getters, or extra logic in the setters. This is one extra layer of abstraction that you can take out of the Class you are instantiating and make the configuration class the data validator.

More of the code I will be releasing on this site will use this implementation so you will get a better idea on how I have adapted this concept through my own projects. I am already using it throughout the Heavy framework I have been building for their new AS 3 player but more on that later…

4 Responses to “AS 3 Config Class”

  1. Jerome Chevreau Says:

    Hi,

    I am learning ActionScript 3 and i was wondering what the public function get …() which then returns a var is for.

    Please will it be possible to help me out?!

    Thanks

    jerome

  2. FlashBum Says:

    Do you not understand how getters and setters work in Flash? I am planning on posting an update to this class along with some of my thoughts on correctly using getters/setters. If you google “Actions Script 3 Getters and Setters” you should find lots of documentation on the subject.

  3. The Flash Art of War » Blog Archive » Config Class part 2 Says:

    [...] So I have been building a new Framework at Heavy. This framework is more designed oriented then the MCVC framework I was using before. The entire UI (Display) tier is built on XML similar to MXML. With that in mind I needed a way to quickly convert xml nodes into config objects. This was the next logical step from my original Config Class. [...]

  4. The Flash Art of War » Blog Archive » Useless Getters/Setters Says:

    [...] is my AS 3 Config class I posted a little while ago here. package com.jessefreeman.configs {         import [...]

Leave a Reply