Skip to end of metadata
Go to start of metadata

Combining the power of both design patterns: Singleton and Factory Method. Singleton produces the same object again where as Factory instantiates new object every time asked.

What is Singleton?

The purpose of the Singleton Design pattern is to return the same object over and over to the caller. Singleton would be useful when you need to have an access to same object in the different part of an application.

What is Factory?

The purpose of the Factory method is to provide a new instance of the object whenever the Factory method is being called. When you are dealing with lot of different interrelated classes, it is better to use the Factory to streamline the object Creation.

What is Singleton-Factory?

Using the Factory method to always get a new object. This Object would be saved in the internal table with the key. If the Key is different than available keys in the Singleton Object collection, we would instantiate a new object by calling the Factory Method.

When do you need to use Singleton Factory?

When you have to use the same object again, but this object should be different based on the different keys. So, if you only call the Factory, you get a new object all the time which would kill the idea of getting the same object for the key. If you only use the Singleton, you get the same object over-and-over. This would also defeat the purpose of using the different object based on the key. In this type of scenarios, when you need to have different objects based on different situation but you may need the old object again later in the game, you should use the Singleton Factory Method.