If you are familiar with the standard GoF Patterns than you more than likely are aware of the Singleton Pattern and the solutions which it provides.
For those of you who are not familiar with the Singleton Pattern it is a Creational Pattern which, when implemented as prescribed ensures only one instance of a class is ever instantiated. This is facilitated via a single global access point from which a singleton instance is to be created and or retrieved.
You may be wondering just what the Singleton Pattern has to do with the Multiton Pattern? And how does the Singleton pattern relate to the Multiton pattern? What are the differences and what are the similarities?
To answer your question the Multiton pattern is a Creational pattern which builds on the concept of the Singleton pattern by adding a mapping of key / value [object] pairs.
Unlike the Singleton Pattern, whereas there is only ever a single instance of an object created, the Multiton pattern ensures that only a single instance of an object is created per key. Therefore there are multiple instances which are managed via the Multiton object. The Multiton pattern provides centralized access of Multiton objects and advocates keyed storage of objects within a system.
Below is a simple example which demonstrates an implementation of the Multiton Pattern in ActionScript 3.0:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package { import com.ericfeminella.utils.HashMap; import com.ericfeminella.utils.IMap; public final class Multiton { private static var instances:IMap = new HashMap(); public function Multiton(access:Private) { if (access == null) { throw new Error( "Abstract Exception" ); } } public static function getInstance(key:*):Multiton { var instance:Multiton=instances.getValue(key); if ( instance == null ) { instance = new Multiton( new Private() ); instances.put( key, instance ); } return instance; } public function get id() : * { return instances.getKey( this ); } } } class Private {} |
Here is a breakdown of the above example.
First a new class is created as well as an additional inner class outside of the package which is used to ensure the constructor can only be called from within the class body, in this case the Multiton class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package { public final class Multiton { public function Multiton(access:Private) { // verify that access is not null // if it is, then an illegal request // to instantiate the constructor // is being attempted if (access == null) { throw new Error( "Abstract Exception" ); } } } } /** * inner class restricting constructor access to private */ class Private {} |
Next a private or protected static var of type HashMap (optionally, a generic Object or Dictionary can be substituted) is defined. The static HashMap instance contains the mappings of keys to objects in the Multiton class. Each key only ever contains a single Multiton object instance, and each Multiton instance can only be accessed by it’s associated key.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package { import com.ericfeminella.utils.HashMap; import com.ericfeminella.utils.IMap; public final class Multiton { // contains key / Multiton instance mappings private static var instances:IMap = new HashMap(); public function Multiton(access:Private) { if (access == null) { throw new Error( "Abstract Exception" ); } } } } /** * inner class restricting constructor access to private */ class Private {} |
Lastly, Multiton implementations require a public static method; getInstance(); which is very similar to the static getInstance() as it applies to the Singleton pattern, but with a slightly different signature. The getInstance(); method in a Multiton requires a single parameter which specifies the key from which a new instance is to be assigned and / or retrieved.
Certain Multiton implementations use an object as the key, however it is arguably more intuitive to use a primitive type such as a String to define keys. Regardless, I prefer not to enforce type restraints as the implementation will typically depend on the context in which it is being applied.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | package { import com.ericfeminella.utils.HashMap; import com.ericfeminella.utils.IMap; public final class Multiton { private static var instances:IMap = new HashMap(); public function Multiton(access:Private) { if (access == null) { throw new Error( "Abstract Exception" ); } } // retrieve the appropriate Multiton instance // if the instance does not currently exist // one will be instantiated and mapped to // the specified key. All subsequent client // requests will return the correct instance public static function getInstance(key:*):Multiton { var instance:Multiton=instances.getValue(key); if ( instance == null ) { instance = new Multiton( new Private() ); instances.put( key, instance ); } return instance; } } } class Private {} |
To implement a Multiton instance all that is needed is to invoke the static getInstance(); on the Multiton class object just as one would invoke getInstance() on a singleton class object. However in the Multiton it is assumed that there will be many instances, albeit controlled instances, therefore a key must be specified.
Below is a simple example which demonstrates how to retrieve a specific instance of a Multiton object:
1 2 3 4 5 6 7 8 9 10 | var multiton1:Multiton = Multiton.getInstance( "a" ); trace( multiton1.id ); // a var multiton2:Multiton = Multiton.getInstance( "a" ); trace( multiton2.id ); // a var multiton3:Multiton = Multiton.getInstance( "o" ); trace( multiton3.id ); // o |
There is not to much documentation on the Multiton Pattern outside of the Ruby community and a Java implementation available on wikipedia, however the Multiton Pattern proves very useful when multiple, controlled object instances are needed.
Thank you this was the only clear explanation that made immediately sense to us. We did look at several websites, (which all they did was adding confusion) and books including Advanced Flex3, Actionscript3 Design Patrrens, and Head First Design Patterns.
Thanks for this article Eric!!!
Why can’t wiki be expressed in this way? Could your article be added to the Multiton Wiki page? That would be a major help!