Manish Sharma
What is the difference between Singleton design pattern and Factory design pattern?
  • abhishek maheshwari
    Nov, 2016 15

    Singleton pattern Singleton pattern is described at per class loader level. Singleton bean scope is per spring container. Spring simply creates a new instance of that class and that is available in the container to all class loaders which use that container. Suppose you have two scenarios: 1. There are multiple class loaders inside the same spring container.. 2. There are multiple containers using same class loader.Factory Method pattern In this pattern, the client (or consumer) asks the Creator (or factory) for a specific type of object from a class hierarchy. The Creator method of the factory class delegates the creation of the specific object to the derived classes and return the object of the class of the type asked by client. In essence, you have a single point of contact for the creation of several objects of a class hierarchy. You can think of this as going to a airline ticket counter (controller) and asking for a ticket by giving your preference of the ticket type (first class, executive or economy). The user is not concerned with how the ticket is being generated, even though in an object representation the first class and the economy ticket are both derived from the base ticket class.Source:-

    • 2
  • Akash Varshney
    Oct, 2015 10

    there are so many differences between Singleton and Factory. both are used for the different purpose..Singleton is used when we need a instance of an object .. and it will only one instance can be created. Factory is used when we need to create instances for different classes at run time.I think .. its not a valid question to ask by an interviewer :)

    • 1
  • Munesh Sharma
    Apr, 2014 17

    A singleton pattern ensures that you always get back the same instance of whatever type you are retrieving, whereas the factory pattern generally gives you a different instance of each type.The purpose of the singleton is where you want all calls to go through the same instance. An example of this might be a class that manages a disk cache, or gets data from a static dictionary; wherever it is important only one known instance interacts with the resource. This does make it less scalable.The purpose of the factory is to create and return new instances. Often, these won't actually be the same type at all, but they will be implementations of the same base class. However, there may be many instances of each type

    • 1

Most Popular Job Functions