Multiton pattern – made me chuckle.

Not sure why I found it funny, but I did. Was reading Stack Overflow today and someone had asked a question about managing an object pool. One answer was, “use the Multiton pattern”.
Has a design pattern ever sounded more like a character from Pokemon? Even though it has a comedy name it does have a legitimate use – whereas a Singleton holds once instance of an object a Multiton holds a specified number and thus creates an object pool. Wikipedia has an example.
On a more serious note, this pattern did come in very useful recently on a JSF/JPA application. Our JSF app uses a number of diferent JPA persistent-unit definitions and we wanted to manage them in one place rather than having to maintain the PU names all over the application. By creating a JpaFactory multiton we were able to hold all EntityManagerFactory instances in one place and avoid passing around PU names.
Similar to the Wikipedia example above, we keep a HashMap<String, EntityManagerFactory> where the key is the PU name.
Subscribe via RSS
RSS via Email
Follow me
Leave a Reply