ActionScript 3.0 Design PatternsI've been reading through O'Reilly's "ActionScript 3.0 Design Patterns: Object Oriented Programming Techniques" by William Sanders and Chandima Cumaranatunge for the last few weeks and have to say its an incredibly useful resource. The interesting thing is that this book approaches design patterns in the more traditional sense, not dumbing down on the object-oriented terminology. In that sense it is very approachable to those coming from a Java or C background and are looking for ActionScript 3.0 implementations of specific patterns.

On the other hand I can't help but feel some patterns are shoehorned to fit in an ActionScript 3.0 context. True, ActionScript is a lot more like Java these days but there are still things that can't directly be translated (or shouldn't necessarily be).

One of my personal pet peeves is the way Singleton gets enforced, not only in this book but various other resources. The issue basically comes down to not being able to declare a constructor as private in ActionScript 3.0, thus no way of preventing the class to be instantiated using the new keyword. One common way to get around this is creating another class in the package declaration (there can only be one public class in your package declaration so it'll be private) and using an instance of that "Singleton enforcer" class as an argument in your Singleton class constructor.

In my view this is an incredibly involved workaround which has its own problems (for example, you're still able to pass null as an argument to the constructor and prevent if from throwing an error). It all comes down to personal preference but my vote goes to Grant Skinner's approach -- he proposed to use a simple Boolean property that defines whether or not the class can be instantiated, it defaults to false so if you try to instantiate the class using the new keyword it throws a runtime error. In the getInstance method, if the private instance needs to be created, the Boolean gets temporarily set to true and back to false after it was instantiated. Admittedly, the one downside with this method is that it throws a runtime rather than a compile time error but its as far as I want to go in creative workarounds to get a pattern to work.

ECMAScript Edition 4 is of course still a work in progress, but I for one can't wait for it to support private and protected constructors and seeing ActionScript implement it.

The number of patterns they've been able to cover in this book is extremely impressive (Factory, Singleton, Decorator, Adapter, Composite, Command, Observer, Template, State, Strategy, MVC, Symmetric Proxy) and all things considered I think they found a balance in staying close to the original design while keeping it readable and straightforward to implement for the typical ActionScript 3.0 audience.

Design Patterns are a pretty dodgy subject to cover with zealots of the 'committee for object-oriented purity' lurking around the corner ready to criticize your every line of code. Kudos to the authors for tackling the subject and making it such an approachable book!

I'd strongly recommend this book to anyone wanting to take a deeper look at the world of Design Patterns and object-oriented architecture.

Other books worth exploring are "Advanced ActionScript 3 with Design Patterns" by Joey Lott, the best selling "Head First Design Patterns" or even my own "Object-Oriented ActionScript 3.0" if you're looking for a non-threatening introduction on the topic.

  My rating: 4 stars

Buy this book on Amazon.com Buy this book on Amazon.co.uk

Posted
AuthorPeter
CategoriesReviews