Jump to content

9.5.1 Creational (Singleton, Factory)

From Computer Science Knowledge Base

9.5.1 Creational (Singleton, Factory)

Creational design patterns are all about how objects are created. They help make sure objects are created in a flexible and organized way.

  • Singleton: Imagine you have a school, and there's only one principal. It wouldn't make sense to have two principals, right? A Singleton pattern ensures that a class can only have one instance (one copy) of itself throughout the entire program. This is useful when you need to control access to a single resource, like a settings manager for a game or a single database connection.
  • Factory: Think of a toy factory. You tell the factory, "I need a car toy," or "I need a doll toy," and the factory builds it for you. You don't need to know all the tiny steps involved in making each toy. The Factory pattern provides a way to create objects without specifying the exact type of object that will be created. It's like having a special method that decides which kind of object to make based on your request, hiding the complex creation process from you.

Bibliography:

  • Gamma, Erich, Helm, Richard, Johnson, Ralph, and Vlissides, John. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional, 1994.