Actionscript3 Design Patterns:The Singleton
by eamonn on Nov.09, 2009, under development
Background Reading
None
The Problem?
You want to ensure you have only a single instance of an object and want global access to it. This may be because the object uses a resource, such as sounds, or because the object needs a lot of configuration, such as a currency formatter, or needs global access, an event dispatcher.
Examples?
Currency formatter, sound controller, global event dispatcher
How?
The Singleton:
package singletonpattern {
/**
* @author eamonn faherty
*/
public class SocketManagerSingleton {
private static var _instace : SocketManagerSingleton;
public static function getInstace() : SocketManagerSingleton {
ensureInstanceExists();
return _instace;
}
private static function ensureInstanceExists() : void {
if (_instace == null) {
_instace = new SocketManagerSingleton();
}
}
public function send(message : String) : void {
}
}
}
Example Usage:
package singletonpattern {
import common.Example;
/**
* @author eamonn faherty
*/
public class SingletonPatternExample extends Example {
public function SingletonPatternExample() {
var socket : SocketManagerSingleton = SocketManagerSingleton.getInstace();
socket.send("Hello world");
}
}
}
Why is it good?
• It works well; if you use an instance protector then you can be sure that only a single instance will exist.
• It is well understood; it is widely used in the actionscript world.
• It is easy to understand; it is a simple pattern that needs little extra code.
• It is easy to implement; actionscript allows us to check the caller of a method and allows us to use private classes.
Why is it bad?
• It tightly couples the classes that use the singleton to the fact that they are using a singleton.
• It is not possible to extend the singleton class if you wanted to add functionality.
• You cannot use your singleton class again; you cannot extend it or reuse it with composition.
• If you are unit testing a class that uses a singleton you need to add some code to your singleton to make testing possible.
Further Reading
Read about the factory and inversion of control patterns.
Related posts:
- Actionscript3 Design Patterns:The Multiton Background Reading Read about the singleton The Problem? You want...
- Actionscript3 Design Patterns:Factory Method Background Reading The creation method pattern The Problem? You want...
- Actionscript3 Design Patterns:Creation Method Background Reading The factory method pattern The Problem? You have...
- Design Patterns for Actionscript 3 I recently did my first public talk! It was at...
- Actionscript3 TDD: Introduction / Terminology I have been posting about design patterns recently and I...
Related posts brought to you by Yet Another Related Posts Plugin.
November 10th, 2009 on 6:22 pm
For me, the singleton has two main flaws. The classes that use the singleton are tightly coupled to the implementation of the pattern and refactoring a singleton into something else is not trivial. The effects of these criticisms can be eased:
Your singleton should implement an interface and all classes that use its public methods should do so using the interface type. This means that you can change the getInstance method call to be another singleton, or something else, and so long as the replacement class implements the same interface you will not have to change any other code.
• You should not use getInstance more than once. Every time you use getInstance you are tightly coupling that method/class to the singleton. Just because the singleton is a singleton it does not mean you cannot pass a reference of the object around.
Above I am passing a reference into my Controller instead of it getting the reference itself. This means that my Controller does not know about the singleton so changing it means that I should not have to change my Controller.