A few days ago, I posted an implementation of a Semaphore with AS3. Ever since then, something about the code just didn’t sit right with me. I’ve wanted to research and write about the Liskov substitution principle (LSP) for a while now, and I decided that I would demonstrate it with my Semaphore classes and examples. When I first designed the classes, I thought I was doing a good job, now….not so much. In fact, I think I made errors in my application architecture and overall approach.
(more…)
[Edit] The source code for this has been updated and can be seen and downloaded on this follow up post.
Flash does not support multihreading, and that’s probably good thing since managing threads can be very, very difficult; however, Flash can have a kind of concurrency when it comes to user interface, multi-user applications (e.g. games) and synchronizing animations. With user interfaces, a developer may wish to disable parts or all of the UI while asynchronous events, such as loading assets and managing various animations are executing. Within a multi-user environment, imagine a game where players share a limited amount of resources – checking things in and out as they become available ( see ‘Dining Philosophers Problem‘). Another scenario might be the need to manage many, many animations.
When it comes to concurrency there are various techniques that come to mind – The Semaphore , Java’s ReentrantLock and CountDownLatch, the Actor model, and Scala’s Mailbox. This first post will focus on the semaphore.
In a previous post, I talked about how I use method closures to avoid AS3 events. Since then I have been using the technique extensively and I still don’t use custom events – at all. The previous example was OK, but this one is better. This is also a class I use in most my projects.
When programming, there are many things I avoid or just squash out altogether:
There are a few examples of undo & redo floating around, but I wanted to give it a shot myself. I didn’t look at anyone else’s solution and I’m glad I didn’t, because I came up with a very different and clean solution.
For some time now, I’ve been using a technique that allows me to skip Actionscript’s events and it’s really been helpful in writing clean code. Recently, prominent community developers such as Andre Michelle, Joa Ebert, Robert Penner, and Nicolas Cannasse have made solid critiques of Actionscript and generally AS3’s event model makes it onto their list of grievances. Andre and Joa have even created UIEvent to help deal with event listeners.
Like many other Flash developers, when I learned about creating custom events or dispatching Flash’s built-in events I started doing it a lot. But….. it can really be a pain. I find that frequently dispatching and cleaning up events really dirties my code. It also makes it harder to read. Also, event dispatchers can be so far removed from methods who are dependent on them, that a coder will often have to search around to find the callback. Because of this I avoid Flash events as much as possible, and here’s how I do it.
[Edit] Callbacks are also substantially faster than events: Grant Skinner’s recent optimization talk
Within the Flash community there has been a fair amount of talk about Dependency Injection. A few ( generally Flex ) frameworks have become popular – notably Robotlegs, Mate and Swiz. It’s easy to find high level discussion about them, but these generally involve developers who are already familiar with DI ( Dependency Injection ) and IoC ( Inversion of Control ).
Fowler’s article is often cited when references are made to DI. Admittedly, it’s a lot to take in and consider, especially when the article gets into containers. All this can be confusing to anyone who’s not a seasoned coder or just wants a simple example and not all that prose.