0
0
Software Engineeringknowledge~30 mins

Creational patterns (Singleton, Factory, Builder) in Software Engineering - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Creational Patterns: Singleton, Factory, and Builder
📖 Scenario: You are learning about creational design patterns used in software engineering to create objects in different ways. These patterns help manage how objects are made, ensuring efficiency and flexibility in programs.Imagine you are designing a simple system that needs to create different types of vehicles. You want to understand how Singleton, Factory, and Builder patterns work by creating examples for each.
🎯 Goal: Build a simple example for each creational pattern: Singleton, Factory, and Builder. You will create a singleton class to ensure only one instance exists, a factory to create vehicle objects based on type, and a builder to assemble a complex vehicle step-by-step.
📋 What You'll Learn
Create a Singleton class called Logger that only allows one instance.
Create a Factory function called vehicle_factory that returns different vehicle objects based on a type string.
Create a Builder class called VehicleBuilder that constructs a vehicle by setting parts step-by-step.
Use clear and simple code to demonstrate each pattern.
💡 Why This Matters
🌍 Real World
Creational patterns are used in software development to control how objects are created, improving code organization and flexibility in real applications like game development, web services, and system design.
💼 Career
Understanding these patterns is essential for software engineers and developers to write maintainable and scalable code, and they are commonly asked about in technical interviews.
Progress0 / 4 steps
1
Create the Singleton class Logger
Write a class called Logger that ensures only one instance can be created. Use a class variable called _instance to store the single instance. In the __new__ method, check if _instance is None; if so, create and store the instance, otherwise return the existing one.
Software Engineering
Hint

Use a class variable to hold the single instance. Override __new__ to control instance creation.

2
Create the Factory function vehicle_factory
Write a function called vehicle_factory that takes a parameter vehicle_type. If vehicle_type is "car", return the string "Car object created". If vehicle_type is "bike", return "Bike object created". For any other value, return "Unknown vehicle".
Software Engineering
Hint

Use if, elif, and else to return different strings based on vehicle_type.

3
Create the Builder class VehicleBuilder
Write a class called VehicleBuilder with an __init__ method that initializes an empty dictionary called vehicle. Add methods set_wheels(self, number) and set_color(self, color) that add these keys and values to the vehicle dictionary. Add a method get_vehicle(self) that returns the vehicle dictionary.
Software Engineering
Hint

Use a dictionary to store vehicle parts. Add methods to set wheels and color, and a method to return the dictionary.

4
Complete the example by creating a vehicle using VehicleBuilder
Create an instance of VehicleBuilder called builder. Use builder.set_wheels(4) and builder.set_color("red") to set the vehicle parts. Finally, create a variable my_vehicle and assign it the result of builder.get_vehicle().
Software Engineering
Hint

Create the builder object, set wheels and color, then get the vehicle dictionary.