Dynamic classes for settings objects

Sometimes real world programming requires solutions that aren’t lovely  OOP-type code. For instance if you have a base class that is extended by a number of children, it’s not always possible to foresee all the types of arguments you need to send them, but you still need to define an interface.

In old-school AS2 code, we often used untyped objects as parameters for constructors. Bad OOP, but as AS2 doesn’t have typing, and the editor we used (SEPY) didn’t show us the arguments for methods and constructors, the code was more readable that way.

In AS3, classes are sealed by default, meaning that no attributes or methods can be added at run-time. This saves memory and increases performance. If you set a class as dynamic, it behaves like classes did in AS2 – you can add whatever you like at run-time.

We found that a variant of this AS2 technique was handy for those indeterminate settings objects. But instead of just passing a generic Object, we can create a dynamic class and define the settings that the base class uses and have those typed strictly. And we still allow future implementations to add their parameters. The downside of dynamic classes is that they use more memory than sealed (default) classes. But as they only live for a short time, it’s not a big real-world problem.

An example class from our framework. Notice the ‘dynamic’ declaration before ‘class’.

canDelete, pageStyle and pageLinkedToNextCount are types and cannot be set with other types. The parseRaw method is a way to take a generic object and insert all its attributes into this class.

package no.papirfly.msp.ptp.templates
     * Defined pageTypeSettings as used in <code>BaseTemplate.initPageTypes</code>  
     * @param canDelete user can delete page. Default is false
     * @param pageStyle
     * @param pageLinkedToNextCount how many pages will be locked to this. 1 = next page is locked to this.package no.papirfly.msp.ptp.templates
    public dynamic class PageTypeSettings
        public var canDelete:Boolean
        public var pageStyle:int
        public var pageLinkedToNexCount:uint;

        public function PageTypeSettings(canDelete:Boolean = false, pageStyle:int = 0, pageLinkedToNexCount:uint = 0)
            this.canDelete = canDelete
            this.pageStyle = pageStyle
            this.pageLinkedToNexCount = pageLinkedToNexCount
       /**        @private         */
        public function parseRaw(o:Object):void {
            for (var item:String in o) {
                this[item] = o[item];
            if (o.canDelete == null) canDelete = false

We can instantiate this class:

var mySettings:PageTypeSettings = new PageTypeSettings(true);

and add new attributes that aren’t declared in the class:

mySettings.specialColorForMyPage = 0xFF00FF;

We lose strict typing, but it’s manageable when you use it for a few classes. If needed, the PageTypeSettings could be extended to type more attributes.


0 Responses to “Dynamic classes for settings objects”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: