« May 2004 | Inicio | July 2004 »

June 14, 2004

AS1 and AS2 at Madrid Underground ( or Metro, or subway )

These are two pictures I've taken this morning, at the Gran Vía Metro station. I found it quite funny.

as1_metro.jpg

as2_metro.jpg

If you want the high resolution images, drop me a line.

June 09, 2004

An example of the proxy pattern

I want to save a list of registered users, but I don't know exactly where to save it. If the user selects a checkbox ( that represents the internet connection ), I'll send the data to a server-side script, in XML format. If the user unchecks the checkbox ( representing that there is not a physical internet connection ), I'll write the data to a Shared Object.

So, this is a mission for the Proxy Pattern!!. Why?. Because I want to isolate the proccess of saving data from the rest of the application, so I just want to say "hey, save this", and don't worry about how ( or where ) it is saved.

That's what this pattern does. The proxy class stores a reference to an object , that will be the real subject of the action. But, the proxy itself creates that object, so it can create an instance of one class or another depending on certain run-time conditions.

So, all the possible classes to be created by the proxy must implement the same interface. And, depending of the kind of proxy, it can be also a good idea to make the proxy class implement also the same interface, so, if needed, it can be replaced.

Some code will help to understand it. First the interface:

interface ISaveable { public function saveUserList( theList: Array ); }

Easy, isn't it?. We want to save a list of users, so we'll have to implement a saveUserList method

Now, the class that handles the "save to server" feature

class myXML extends XML implements ISaveable { private var userArray: Array; function myXML( ) { this.userArray = new Array( ); } function sendData( ) { //this.sendAndLoad( ); } public function saveUserList( theList: Array ) { trace( "The XML is handling the data" ); this.userArray = theList; //create the XML nodes, and send to server } }

Not too complicated. Just a public method to receive an array, and then I'll have to create the XMl nodes, and send the XML to the server.

Well, now the class that saves data to disk

class mySO implements ISaveable { private var theSO: SharedObject; private var userArray: Array; function mySO( ) { this.userArray = new Array( ); } public function saveUserList( theList: Array ) { trace( "The Shared Object handles the data" ); this.userArray = theList; } }

Again, this class implements ISaveable, so after receiving the data, it will try to save it to the contained Shared Object

Now, the tricky part. The value of the checkbox ( the connection status ) is represented here by a variable placed at _root. But, to get this variable value, I'll use a Singleton ( here you can find an implementation by Dave Yang ), That's just a way to have a global accesor to that value, but I prefer it that just using globals. Here is the singleton:

class singleton { private static var inst:singleton; private function singleton() {} public static function get instance():singleton { if (inst == null) inst = new singleton(); return inst; } public function isConnected( ): Boolean { return _root.connected; } }

And finally, the proxy class

class saveUserListProxy implements ISaveable { private var realClass: ISaveable; function saveUserListProxy( ) { var config: singleton = singleton.instance; var conFlag: Boolean = config.isConnected( ); if ( conFlag ) { this.realClass = ISaveable( new myXML( ) ); } else { this.realClass = ISaveable( new mySO( ) ); } } public function saveUserList( theList: Array ) { this.realClass.saveUserList( theList ); } }

So, finally, for this example, in the main timeline, we'll have

var connected: Boolean = false; var theUserList: Array = new Array( ); theUserList.push( { id: 0, name: "Cesar" } ); theUserList.push( { id: 1, name: "Javier" } ); var myProxy: saveUserListProxy = new saveUserListProxy( ); myProxy.saveUserList( theUserList );

And that's all. Test it changing the values of the variable "connected". And sorry for my poor english. It's been particularly poor tonight :(