Complete the code to apply the Single Responsibility Principle (SRP) by defining a class that only handles user data storage.
class UserDataHandler: def __init__(self, user): self.user = user def [1](self): # Save user data to database pass
The Single Responsibility Principle means a class should have one job. Here, the method should only save user data, so save_user_data is correct.
Complete the code to follow the Open/Closed Principle by allowing new payment methods without changing existing code.
class PaymentProcessor: def process(self, payment_method): if payment_method == 'credit_card': self.process_credit_card() elif payment_method == 'paypal': self.process_paypal() # To add new methods, use [1] instead of if-else
The Open/Closed Principle says software should be open for extension but closed for modification. Using polymorphism allows adding new payment methods without changing existing code.
Fix the error in the code to respect the Liskov Substitution Principle by ensuring the subclass can replace the superclass without issues.
class Bird: def fly(self): print('Flying') class Penguin(Bird): def [1](self): print('Penguins swim!')
The Liskov Substitution Principle means subclasses should be replaceable for their base classes without breaking expected behavior. Overriding fly with penguin-appropriate behavior (like swimming) instead of raising an exception respects substitutability.
Fill both blanks to apply Interface Segregation Principle by splitting a large interface into smaller ones.
class [1]: def read(self): pass def write(self): pass class [2]: def print(self): pass
The Interface Segregation Principle says clients should not depend on methods they do not use. Splitting a large interface into Readable and Printer interfaces helps achieve this.
Fill all three blanks to apply Dependency Inversion Principle by depending on abstractions, not concrete classes.
class [1]: def connect(self): pass class DatabaseConnection([2]): def connect(self): print('Connected to DB') class Application: def __init__(self, db: [3]): self.db = db
The Dependency Inversion Principle says high-level modules should depend on abstractions. Here, IDatabase is the abstraction, DatabaseConnection is the concrete class, and Application depends on the abstraction IDatabase.