What if you could stop repeating yourself and make your software smarter about creating things?
Why Creational patterns (Singleton, Factory, Builder) in Software Engineering? - Purpose & Use Cases
Imagine you are building a complex software system where you need to create many objects, like different types of vehicles or database connections, by hand every time you need them.
You write code everywhere to create these objects, making your program messy and hard to manage.
Manually creating objects everywhere leads to repeated code, mistakes, and confusion.
It's easy to accidentally create multiple instances of something that should only exist once, or to mix up how objects are built.
This slows down development and makes fixing bugs harder.
Creational patterns like Singleton, Factory, and Builder organize object creation in a smart way.
They let you control how and when objects are made, ensuring consistency and reducing errors.
This makes your code cleaner, easier to understand, and simpler to change.
Car car1 = new Car("red", "sedan"); Car car2 = new Car("blue", "sedan"); // repeated everywhere
Car car1 = CarFactory.createSedan("red"); Car car2 = CarFactory.createSedan("blue"); // centralized creation
It enables building flexible, reliable software where object creation is clear, controlled, and reusable.
Think of a coffee shop where the barista uses a standard recipe (Builder) to make different drinks, a manager ensures only one cash register is used (Singleton), and a menu helps choose the right drink maker (Factory).
Manual object creation is repetitive and error-prone.
Creational patterns organize and simplify how objects are made.
They improve code quality, maintainability, and flexibility.
Practice
Solution
Step 1: Understand the Singleton pattern purpose
The Singleton pattern restricts the instantiation of a class to one object only.Step 2: Compare with other patterns
Factory creates objects but does not limit the number; Builder constructs complex objects step-by-step; Prototype clones objects.Final Answer:
Singleton -> Option AQuick Check:
Only one instance = Singleton [OK]
- Confusing Factory with Singleton
- Thinking Builder limits instances
- Mixing Prototype with Singleton
Solution
Step 1: Recall Singleton implementation details
Singleton classes hide their constructor (make it private) to prevent direct instantiation.Step 2: Provide controlled access
A static method returns the single instance, creating it if it doesn't exist yet.Final Answer:
Make the constructor private and provide a static method to get the instance -> Option CQuick Check:
Private constructor + static getter = Singleton [OK]
- Allowing public constructors in Singleton
- Using factory method to create multiple instances
- Confusing Builder with Singleton syntax
class AnimalFactory {
createAnimal(type) {
switch(type) {
case 'dog': return new Dog();
case 'cat': return new Cat();
default: return null;
}
}
}
const factory = new AnimalFactory();
const pet = factory.createAnimal('dog');
console.log(pet.sound());What will be the output if
Dog.sound() returns "Woof" and Cat.sound() returns "Meow"?Solution
Step 1: Understand the Factory method call
The factory creates an object based on the string 'dog', so it returns a new Dog instance.Step 2: Call the sound method on the Dog instance
Since Dog.sound() returns "Woof", the console.log outputs "Woof".Final Answer:
"Woof" -> Option DQuick Check:
Factory creates Dog, Dog.sound() = "Woof" [OK]
- Assuming factory returns null for known types
- Confusing Dog and Cat outputs
- Thinking createAnimal is undefined
class Logger {
constructor() {
if (!Logger.instance) {
Logger.instance = this;
}
return Logger.instance;
}
}
const log1 = new Logger();
const log2 = new Logger();
console.log(log1 === log2);What is the output and what is the issue with this Singleton implementation?
Solution
Step 1: Analyze the constructor logic
The constructor checks if Logger.instance exists; if not, assigns this to it, then returns Logger.instance.Step 2: Check the instance equality
Both log1 and log2 refer to the same Logger.instance, so log1 === log2 is true.Step 3: Identify the issue
The constructor is still public, so new Logger() can be called multiple times, which is not ideal for Singleton pattern.Final Answer:
true; but constructor is public which is not ideal -> Option BQuick Check:
Same instance but public constructor = A [OK]
- Assuming public constructor is fine for Singleton
- Thinking output is false due to new calls
- Expecting runtime error on instance assignment
Solution
Step 1: Understand the problem requirements
The object is complex with many optional parts, so building it step-by-step is needed.Step 2: Match with creational patterns
Builder pattern is designed to construct complex objects step-by-step, allowing optional parts without confusing client code.Step 3: Exclude other patterns
Singleton limits instances, Factory creates objects but not stepwise, Prototype clones existing objects.Final Answer:
Builder -> Option AQuick Check:
Step-by-step complex object = Builder [OK]
- Choosing Singleton for complex object creation
- Using Factory when stepwise building is needed
- Confusing Prototype cloning with building
Ready to prove it?
Pass the test and this pattern moves unknown
