Complete the code to choose the creational pattern that provides a single instance.
class DatabaseConnection: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) return [1]
The Singleton pattern ensures only one instance exists. Returning cls._instance provides that single instance.
Complete the code to use the Factory Method pattern to create objects without specifying the exact class.
class AnimalFactory: def create_animal(self, animal_type): if animal_type == 'dog': return Dog() elif animal_type == 'cat': return [1]()
The Factory Method creates objects based on input. To create a cat, return Cat().
Fix the error in the Builder pattern code to correctly build a product step by step.
class CarBuilder: def __init__(self): self.car = Car() def add_wheels(self): self.car.wheels = 4 def add_engine(self): self.car.engine = 'V8' def get_car(self): return [1]
The method should return the built car object stored in self.car.
Fill both blanks to complete the Abstract Factory pattern that creates families of related objects.
class GUIFactory: def create_button(self): return [1]() def create_checkbox(self): return [2]()
Abstract Factory creates related objects. For Windows GUI, create WindowsButton and WindowsCheckbox.
Fill all three blanks to complete the Prototype pattern code that clones objects.
class Document: def __init__(self, content): self.content = content def clone(self): return Document([1]) original = Document('Report') copy = original.clone() copy.content = [2] print(original.content, copy.content) # [3]
The clone method copies the content from self.content. Then we change the copy's content to 'Summary'. The print shows original and copy contents.