Archive

Posts Tagged ‘tutorial’

Cairngorm – the Way I See It – Third Part

August 19th, 2009 1 comment

To put together the last part of the Cairngorm series, I’ve started doing a little research about the Cairngorm architecture and got myself into trouble : I got lost into debates! All over the place there are voices which express a “this architecture versus that one“, who points out the pro and con for each and every single principle applied there. I don’t like debates, at least not in the sense of “mine is bigger than yours“, so, I feel that it is my duty to remind you the following:

  • if you are reading this series of articles, then you got here looking for tutorials, right? You are looking for a way to discipline / educate yourself, to get started with the design patterns terms (in a practical sense) and to make your first mistakes – from which you will surely learn;
  • do not get discouraged by those voices I’ve mentioned : criticism is good as long as you take what you need from it (and I’ll give you an example later regarding Singleton debates). To consider a bad thing that Cairngorm relies too much on the binding seems stupid to me, since points out another debate about how good is binding and why you shouldn’t use it. Another issue which is considered to be very important is monolithic classes : nobody forces you to get there, but you just might run into this problem. The solution ? Refactor your classes grouping by functionality and roles – as the basic Cairngorm principle says;
  • take a good look at Universal Mind Cairngorm Extensions before starting to play with Cairngorm;
  • Cairngorm 3 is quite recent – so Cairngorm is not (yet) deprecated, even if the alternatives seems better (by alternatives I mean pureMVC, Mate, Parsley, etc);
  • if you haven’t done so until now, you should take a look to the official article which introduces you into Cairngorm.

Since I’ve mentioned the debate over Singleton, I’m inviting you to read this and then this. I’m not going to take sides over this debate (too) – I’m so in love with Singleton and so far from puristic approach to design pattern. As long as you are carefully about excessive use of Singletons and you strive to respect the loose coupling between classes, you will be fine – and the day when Singletons will disappear from your code is not that far.

Now, there are two things that you will need in order to complete your Cairngorm terms : one is the Mediator. Shortly said a mediator is a class two other classes, so those two won’t couple themselves. You can find a way (and the explanation) to do it here.

Also, you will need some sort of substitute for the Observer pattern. The closest class to that is the following (the code is not my creation – I took it from somewhere, but I cannot remember where – my apologize to the author):

public class Observe extends Observer {
		private var _handler : Function;
		private var _source : Object;
		override public function get handler() : Function {
			return _handler;
		}
		public function set handler( value : Function ) : void {
			_handler = value;
			if( value != null ) {
				isHandlerInitialized = true;
				if( isHandlerInitialized && isSourceInitialized ) {
					callHandler();
				}
			}
		}
		override public function get source() : Object {
			return _source;
		}
		public function set source( value : Object ) : void {
			_source = value;
			isSourceInitialized = true;
			if( isHandlerInitialized && isSourceInitialized ) {
				callHandler();
			}
		}
	}
 
public class Observer {
 
		protected var isHandlerInitialized : Boolean = false;
		protected var isSourceInitialized : Boolean = false;
 
		public function get handler() : Function {
			return null;
		}
 
		public function get source() : Object {
			return null;
		}
 
		public function execute( method : Function, ...params : Array ) : Object {
			var returnValue : Object;
			try {
				returnValue = method.apply( null, params );
			}
			catch( e : Error ) {
				trace( e.message );
			}
			return returnValue;
		}
 
		protected function callHandler() : void {
			try {
				handler.call( null, source );
			}
			catch( e : Error ) {
				trace( e.message );
			}
		}
	}

How you will use it ? Simple : let’s say you need to observe some property of a instance child. You’ll use binding to set the Observe source property and declare a handler for that (e.g. :  < mx: Observe source = ” { someChild.someProp } ” handler = ” onChildPropChanged () ” / > ). As you can see, this is not a design pattern, but a trick to achieve as close as possible the same effect.

Last but not least, you’ll have to know about you might bump into while using Cairngorm:

  • you will not be happy about not being able to notify events to the view. Dispatching events from commands and listening them in the view seems like a good idea – but after a degree of complexity this will surely get in your way. Writing a Singleton which can cover the register-me-for-event the way FrontController is doing it might be a much clearer solution – of course, keeping them separately so you can easily locate them.
  • you will be angry with the number of small classes containing commands and events. There are workarounds for this one that you can google. My technique is to declare all the event names in a static manner and use only the CairngormEvent, without creating more classes – you’ll get annoyed by the type casting anyway. Another thing that you can quick to is to extend the CairngormEvent to add a class property so the getter of data will type cast itself (as I’ve said in part II).
  • on large applications, use case handling and reacting to state becomes overwhelming for the number of views you have to cover – very hard when you have to debug. Making one big view could help you with that – also you might want to introduce new design patterns for this, e.g. State Machine.

Of course, this is not everything that might get into troubles, just what I could recommend you right now – but I would like to hear from you if you need my help with something.

Before ending this post, I’m recommending you to read an article about PureMVC – of course, once you’ve absorbed all there is to know from Cairngorm.

The simpliest way to launch another AIR application from an AIR application

May 25th, 2008 2 comments

I’ve searched the web for a method of launching another AIR application from an AIR application and the only way was to load a chunk of SWF, like David does here.

The main reason for searching the simplest way is based on the fact that AIR supposed to be desktop application and to take into consideration that the Internet connection might be unavailable at some point. So, how to launch another application if there is no Internet connection to bring the air.swf file from Adobe’s server?

Just like that:

1. You know the appID of the application your are trying to launch

2. You know the publisherID of that application too

3. Import the following undocumented class adobe.utils.ProductManager (import adobe.utils.ProductManager);

4. Write where you need the following:

myLauncher = new ProductManager();

var myArguments:Array = ["-launch", appID, publisherID, theOtherAppArguments];

myLauncher(myArguments.join(” “));

That’s it! Now you can fast launch your other application, without needing the air.swf piece that will do this for you. Of course, if you need to do checks (like if that another application is installed) you will need to write more code and do your own research on this undocumented class.

Flex 4 (Gumbo) wish list

March 7th, 2008 No comments

I was invited by Mihaela Barbu to Adobe Romania : we will talk about how I use Flex, what’s so God damn good about it, and most important, my Flex 4 wish list. Since the meeting is next week, I’ll have to put together a list – I usually write this kind of lists on my desktop or talk about them to my beloved one (someday I’ll buy her diamonds for the patience of listening me talking like a mad scientist). This time I will do it different, having the proof over time beside me – and hoping that all points will be checked (did you saw The Bucket List?).

Now seriously, why would a Flex developer complete such a list, immediately after Flex 3 SDK was released? And you see, all that comes into my mind is related to Flex Builder because as a developer, if I need something and the SDK permits it, I’ll just start building. Ok, there are some parts – like garbage collection – which will go to this wish list. So here is the thin list, three ways:

A. Wishes for the Adobe Flash Player

  • #1. Chronologically, the first unresolved issue is the black application controls (there is no possibility to set the text selection color). I wish that the Flash player will let the developer choose the text selection color, or present an video inverse color scheme to avoid the workaround.
  • #2. Encouraging the use of PDF : how nice it will be when the Flash player will contain code for saving PDF instead of printing and all that from a Flex application? I’m sure that the day when this is possible without needing third party software, will be a D-day for the Flex developers and Flex applications. I guess you’ve got my point…
  • #3. This point is more about the wrapper, but is a very common scenario nowadays : since Internet Explorer refuses to die and I, developer, consider that more than 80% of the users around the world don’t know how to use their browsers, I wish that the wrapper somehow detect if someone’s IE doesn’t permit add-ons being started in “safe-mode”. I used quotes with safe-mode since there is no safe mode of using IE – start using FireFox now!
  • #4. Access to the garbage collection please! Not only in the debugger. For the developers who really knows what are they doing – and I promise to became one soon after this feature is released – I know that this will be like getting hands on their own objects, feeling close to God and C++

Observation : I agree with the wish list (including some comments) published here.

B. Wishes for Flex SDK – this is really short

  • #1. A better RichTextEditor – my guess is that it is still limited due to the sandbox, which makes the developer upload the picture before we can insert it into an RichTextEditor (with a custom control, of course). Using XHTML? :)
  • #2. Page-able collections – in a more elegant manner than today.

Observation : I wish (for myself) to design applications which makes better use of the Model from the MVC – I’ll get back to this idea in a future post. The list for the SDK is short for a simple reason : we develop! Therefore, this wish list is constantly checked and rewritten.

C. Wishes for Flex Builder

  • #1. internationalization helper tool – you select an STR_ID and call through a shortcut a wizard which inserts the same STR_ID into properties files (you’ve had let system know about) letting you complete the string after equal in all properties files;
  • #2. test area – which lets you test a project (even a chunk of code), without create new project, without having to define a lot of stuff to see the Flex code running (generally, it will be used for taking a bug out of the system and treat it, or for optimization purposes – when you copy-paste a chunk of your code).
  • #3. the coder has this area where he can collect all sort of tests in snippets and even in bug-solving collections which can be submitted to his personal blog or to a community site…
  • #4. command line parameters wizard : every time I want to do something (complex?) with the command line I’ll have to check an external source for parameters (and I guess is not hard to build)

Among this list I should have written about documentation and samples. The great mistake about documentation is the manner in which it is organized. First, I wish there was a TextInput which filters a list of class names (I scroll to much and I’m getting tired, eventually bored and start looking somewhere else). Second, I wish there was a map (like cheat sheets has), made in Flex, in which you can go inside component methods and properties – lets say using the SpringGraph. Also, I’m imagining the day that documentation will talk about technique (and I’m not referring only Flex, but most of the good languages) – I agree, that’s the role of community and this is how you can find good inspirational websites, which explain “how you should do it!” with a bit of “why this way”.

I will get back on this subject, mostly with wishes for the SDK.