Difference between singleton and factory method pattern

The Singleton pattern and the Factory Method pattern are both creational design patterns but serve different purposes and have distinct characteristics:

Singleton

The singleton pattern is an often used pattern in many applications when only a single instance of a resource is required. The most obvious type of resource for PHP web pages is a database connection, although other resource types can be used. When fetching or dynamically creating a web page several database calls may need to be made. If a single instance of the resource could be used, rather than creating several connections, the overhead is minimized. The single instance, in this case, is created by the singleton pattern.

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 that 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 only one known instance must interact with the resource. This does make it less scalable.

  • Purpose: Ensures a class has only one instance and provides a global access point to that instance.
  • Key Features:
    • Private constructor to prevent instantiation from other classes.
    • Static member variable that holds the single instance of the class.
    • Static method (getInstance()) to access the singleton instance.
  • Usage Scenarios:
    • When you need exactly one instance of a class available across the entire application.
    • Examples include configuration classes, logging classes, database access objects, etc.

Factory

The factory pattern is a class that creates objects for you, rather than you needing to use the new keyword to create one yourself. The factory is, as the name suggests, a factory for creating objects.

The purpose of the factory is to create and return new instances. Often, these won’t 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

  • Purpose: Defines an interface for creating objects, but allows subclasses to decide which class to instantiate. It defers instantiation to subclasses.
  • Key Features:
    • Abstract Creator class or interface that declares the factory method.
    • Concrete Creator subclasses implement the factory method to create objects of a specific type (often subclasses of a common superclass or interface).
  • Usage Scenarios:
    • When there are multiple subclasses of a class the exact subclass to be used is decided at runtime.
    • Promotes loose coupling by allowing the creation of objects to be handled by subclasses or external classes.

 

Key Differences:

  • Purpose: Singleton ensures a single instance of a class, while Factory Method delegates the responsibility of object instantiation to subclasses.
  • Usage: Singleton is used when you want to ensure only one instance of a class exists, whereas Factory Method is used when you want to delegate the object creation to subclasses, allowing flexibility in which subclass is instantiated.
  • Structure: Singleton typically has a single class with a static method to access the instance, whereas Factory Method involves an interface or abstract class with a method that subclasses implement to create objects.
SingletonFactory
Returns the Same instanceReturns various new instances
Single Constructor hidingMultiple Constructors expose
No interfaceInterface driven
No SubclassesSubclasses

 

Summary

In summary, while both patterns deal with object creation, they serve different needs: Singleton ensures single instance control, while Factory Method promotes flexible object creation through subclassing.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments