Whenever I write a class, I typically write a usage example prior to writing the class itself. I want to understand what the class will be like to work with, its behavior/interface in development. With OOP it is so easy to hop right into building class after class but actually seeing usage really helps me get the interface right and makes development much easier. On Thursday, before writing the class version of f.net.Load, I showed Doug Winnie how this might work as an example. He notices I hadn’t written the classes yet and that it would be a great feature to actually generate classes from usage. hmmmm…
So this idea of “Usage Modelling” has been stuck in my head for a few days. How could I write usage examples with modelling classes and generate classes the support this behavior? Instead of UML or modelling could one use usage examples to generate classes? It wouldn’t generate the logic within methods but would generate a very solid start to a set of classes. Basically writing code to generate better code.
This is possible with flash.utils.Proxy where you can catch all interface interaction on a class and simply send class text to the output panel. Based on the type passed in methods or get/set behavior you can type the class properties dynamically, setting a string would type a property as a string. Here is some some “Usage Modelling” usage:
import f.model.UsageModel;
UsageModel.start();
//model the events Class
LoadEvent:* = UsageModel.create( ‘f.event.LoadEvent’ , flash.event.Event );
//consts in caps
LoadEvent.OPEN = ‘f.events.LoadEvent.OPEN’;
LoadEvent.CLOSE = ‘f.events.LoadEvent.CLOSE’;
LoadEvent.PROGRESS = ‘f.events.LoadEvent.PROGRESS’;
LoadEvent.SUCCESS = ‘f.events.LoadEvent.SUCCESS’;
LoadEvent.FAIL = ‘f.events.LoadEvent.FAIL’;
LoadEvent.INIT = ‘f.events.LoadEvent.INIT’;
//instance use
var loadevent:* = new LoadEvent( LoadEvent.FAIL );
loadevent.data = {};
loadevent.loader = {};
loadevent.percent = 0.75;
loadevent.bytesLoaded = 34;
loadevent.bytesTotal = 56;
loadevent.bytesAvailable = 56;
loadevent.error = ‘woops’;
loadevent.status = ‘status 23′;
//denote start of Class
Load:* = UsageModel.create( ‘f.net.Load’ , flash.events.EventDispatcher );
//define class data
Load._Public = true;
//static use
Load.AMF = “f.net.Load.AMF”;
Load.amf( LoadModel.AMF , “http://onflex.org/f/Load/test.amf” , function(){} );
//instance use
var load:* = new Load();
load.url = “http://onflex.org/f/Load/test.amf”;
load.parameters = { method:’post’, data:{ a:12345 }};
load.resultFormat = Load.AMF;
load.addEventListener( LoadEvent.SUCCESS , loadSuccess );
load.addEventListener( LoadEvent.PROGRESS , loadProgress );
load.addEventListener( LoadEvent.FAIL , loadFail );
load.addEventListener( LoadEvent.OPEN , loadFail );
load.addEventListener( LoadEvent.CLOSE , loadFail );
load.load();
UsageModel.print();
And no I haven’t written any classes yet, the irony.
Cheers,
Ted

