0
0
Software Engineeringknowledge~10 mins

Creational patterns (Singleton, Factory, Builder) in Software Engineering - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Creational patterns (Singleton, Factory, Builder)
Start
Need object creation
Singleton
One instance
Use object
End
The flow starts when an object is needed. You pick one of the three creational patterns: Singleton for one instance, Factory for creating objects based on input, or Builder for step-by-step construction. Then you use the created object.
Execution Sample
Software Engineering
class Singleton:
    _instance = None
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance
This code creates a Singleton class that ensures only one instance exists.
Analysis Table
StepActionConditionResultInstance ID
1Call Singleton()_instance is NoneCreate new instanceID_1
2Call Singleton() again_instance is not NoneReturn existing instanceID_1
3Call Singleton() third time_instance is not NoneReturn existing instanceID_1
ExitNo more calls-All calls return same instanceID_1
💡 Singleton pattern stops creating new instances after the first one.
State Tracker
VariableStartAfter 1After 2After 3Final
_instanceNoneID_1ID_1ID_1ID_1
Key Insights - 3 Insights
Why does Singleton return the same instance every time?
Because after the first creation (_instance is set), the condition in step 2 and 3 returns false for creating new, so it returns the stored instance (see execution_table rows 2 and 3).
How does Factory pattern decide which object to create?
Factory uses input or parameters to choose which subclass or object to create, unlike Singleton which always returns one instance (not shown in table but conceptually different).
Why use Builder instead of directly creating an object?
Builder allows step-by-step construction of complex objects, useful when many parts or configurations are needed, unlike Singleton or Factory which create objects in one step.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the instance ID returned at step 3?
AID_1
BID_2
CNone
DNew instance created
💡 Hint
Check the 'Instance ID' column at step 3 in the execution_table.
At which step does the Singleton pattern create a new instance?
AStep 3
BStep 2
CStep 1
DExit
💡 Hint
Look at the 'Result' column to see when 'Create new instance' happens.
If the _instance variable started as ID_5 instead of None, what would happen at step 1?
ACreate new instance with ID_1
BReturn existing instance ID_5
CError because _instance is not None
DCreate multiple instances
💡 Hint
Refer to variable_tracker and execution_table step 1 condition checking _instance.
Concept Snapshot
Creational Patterns:
- Singleton: One instance only, reused everywhere.
- Factory: Creates objects based on input type.
- Builder: Builds complex objects step-by-step.
Use Singleton to save resources, Factory for flexible creation, Builder for complex setups.
Full Transcript
Creational patterns help create objects in software. Singleton ensures only one instance exists by checking if an instance already exists before creating a new one. Factory creates different objects based on input, allowing flexibility. Builder constructs complex objects stepwise, useful when many parts or configurations are needed. The execution table shows Singleton creating one instance at step 1 and returning the same instance later. Variables track the instance state. Key moments clarify why Singleton returns the same instance and differences with Factory and Builder. Quizzes test understanding of instance creation and reuse.