YOUR FEEDBACK
Rapid Module Development for DotNetNuke
MICHEAL SMITH wrote: GO TO THE LINK, U HAVE EVERYTHING U WANT THERE. MICHEAL...


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
MXDJ TOP LINKS YOU MUST CLICK ON !


ActionScript 3: Dynamic classes
Prudent Java O-O Programmers, please take a look at this

Digg This!

From Farata Systems blog

In Java, if you’ve created an object from a particular class, you can use only properties and methods that were defined in this class. This is not the case in ActionScript 3. This is one of the examples of unusual (from the OOP perspective) programming techniques. For example, if the following  class:

class Person {
   String name;
}

you can only manipulate with the name property:

Person p = new Person();
p.name = “Joe”;
System.out.println(p.name);

ActionScript calls such classes sealed, but it also has different  animals: dynamic classes, which allow you to programmatically add new properties and behavior to classes during the run-time. Just add the magic keyword dynamic to the class definition:

dynamic class Person {
   var name:String;
}

Now let’s add dynamically two variables name and age and the function printme() to the object of type Person:

Person p= new Person();
p.name=”Joe”;
p.age=25;
p.printMe = function () {
    trace (p.name, p.age);
}
p.printMe(); // Joe 25

You do not have complete freedom though: you can dynamically add only public properties and methods. Of course, nothing comes for free  and sealed classes are a bit more efficient in terms of memory consumption, because they do not need to create a hash table to store the properties and methods that are unknown during compilation. Another obvious restriction is that dynamically added functions can’t access private members of the dynamic class. Read the blog Programming In Style or an Elevator Pitch  to see how by just declaring standard Flex component dynamic, the your code becomes more simple and elegant.

In AS3, any function can be attached to a dynamically created property  of a dynamic  object, for example

function calcTax():Number {…}

var myObject:SomeObject = new SomeObject();
myObject.tax=calcTax;  //add the tax property and attach the function calcTax()
var myTax=myObject.tax();

The delete operator destroys the property of an object and makes it eligible for garbage collection:

delete calcTax();
myTax=myObject.tax()   // generates an error


Some of the Flex classes were defined as dynamic, i.e. Object, Array, MovieClip, NetConnection, TextField, and others. At the time of this writing, subclasses of dynamic classes are not dynamic by default.
Because of this, you may run into an ugly run-time error: imagine a sealed class S that extends a dynamic class D. If you create an object as
D myObj =  new S(), an attempt to add a propery to myObj will produce a runtime error because the variable myObj points at a sealed object.

Let’s do a quick test. Create a new project in FlexBuilder and select  ActionScript project as its type. Enter AS_Only_Project  as the project name. In a couple of seconds you’ll see the auto-generated code that looks as follows:

package {
    import flash.display.Sprite;

    public class AS_Only_Project extends Sprite
    {
        public function AS_Only_Project()
        {
        }
    }
}

Next, create a new class called D and check odd the Dynamic checkbox in FlexBuilder pop-up. You’ll get this class.

package {
    public dynamic class D
    {
    }
}

Now, instantiate and test the dynamic nature of the class D by adding the constructor
        public function AS_Only_Project()
        {
            var myD:D=new D();
            myD.favoriteBand="Pink Floyd";
            trace("Favorite Band="+myD.favoriteBand);
        }
Run this application in the debug mode, and sure enough it’ll print

Favorite Band=Pink Floyd

Create one more sealed class  called S inherited from the dynamic D:
package {
    public class S extends D
    {
    }
}

An attempt to add properties on the fly to the instance of the class S fails miserably as expected:

            var myS:D = new S();
            myS.favoriteSinger="Alla Pugacheva";
            trace("Favorite Singer="+myS.favoriteSinger);

ReferenceError: Error #1056: Cannot create property favoriteSinger on S.
    at AS_Only_Project$iinit()[C:\TheRIABook\eclipse\AS_Only_Project\AS_Only_Project.as:13]

If you’ll try to instantiate your sealed class as follows:

        var myS:D = new S() as D;

I have two news for you: the good news is that it compiles, and the bad (and expected) news is that it generates exactly the same runtime error.

Most likely Adobe’s gonna hire a hitman and kill me after the following statement, but I’m going to say it anyway (at least you’ll now who to blame)… May be I should not?…Life is so good, and I’d like to witness the success of Apollo…I’m sayyyiiiinng this:

If you need to add new functionality to one of the existing standard Flex components (buttons, comboboxes and the like), do not bother extending them and creating new classes. Just create one simple empty subclass with the keyword dynamic and instantiate and add new properties on the fly as needed, as was shown in the Smalltalk version in this article .  

A sound of a silenced pistol shot. Curtain.

About Yakov Fain
Yakov Fain is a managing principal of Farata Systems, consulting, training and product company. He has authored several Java books, dozens of technical articles. SYS-CON Books released his latest co-authored book , "Rich Internet Applications with Adobe Flex and Java: Secrets of the Masters" in Spring 2007. Sun Microsystems has nominated and awarded Yakov with the title Java Champion. He leads the Princeton Java Users Group. Yakov teaches Java and Flex 2 part time at New York University. He is an Adobe Certified Flex Instructor and an Editor-in-Chief of Flex Developers Journal.

LATEST FLEX STORIES & POSTS
Two great PDF creators
I like reading stuff in pdf format. But it's even better if you can easily create pdf files. By easily I mean a button click. Literally.Since I have Adobe Acrobat, my Microsoft Word and PowerPoint just have an extra menu to create it. But it's kinda boring. Let me share with you a cou
3rd International Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
A Runtime Integration Approach to Application Development
This pattern is a hybrid of plug-in and event-driven architecture to integrate individual plug-ins together to come up with the Plug-in Integrator Pattern. This pattern leverages the benefits of both these well-known architectures to provide an optimal solution to build an enterprise-r
JavaOne 2008: Sun Talks Up its Late-to-the-Party AIR-Silverlight Rival
At Java One this week Sun has been selling its year -old-but-still-upcoming - and definitely late-to-the-party - Adobe AIR- and Microsoft Silverlight-competitive JavaFX Rich Client environment as a potential revenue-generator capable of putting ads on mobile applications and JavaFX Scri
Facelift Your SOA with Rich Internet Applications
We are entering an era of Rich Internet Applications (RIA) and enhancing the user experience of consumers of the services becomes an important part in designing and implementing SOA. But if you decide to develop rich clients, you'll be facing the dilemma - which way to go - remain with
Adobe Flash Player 10 Public Beta Now on Adobe Labs
Today, Adobe announced the immediate availability of Adobe Flash Player 10 beta as a free download from Adobe Labs. Adobe Flash Player 10 beta, code named 'Astro', builds on the capabilities of the world's most ubiquitous application runtime with new support for custom filters and effe
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS
SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON FEATURED WHITEPAPERS

ADS BY GOOGLE