Sobes.tech
Middle

What types of creational patterns do you know?

sobes.tech AI

Answer from AI

Creational design patterns are responsible for object creation and managing the instantiation process. The main types of creational patterns:

  • Singleton — ensures that a class has only one instance and provides a global point of access to it.
  • Factory Method — defines an interface for creating an object but allows subclasses to alter the type of objects that will be created.
  • Abstract Factory — provides an interface for creating families of related or dependent objects without specifying their concrete classes.
  • Builder — separates the construction of a complex object from its representation, allowing the same construction process to create different representations.
  • Prototype — creates new objects by copying existing ones.

Example of Singleton in Python:

class Singleton:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

s1 = Singleton()
s2 = Singleton()
print(s1 is s2)  # True