Which scenario best exemplifies the Dependency Inversion Principle?
A 'Car' class directly instantiates and uses an 'Engine' class.
A 'Logger' class writes logs directly to a file.
A 'DataProcessor' class depends on an 'IDataSource' interface, not a specific database implementation.
A 'LightSwitch' class is directly dependent on a 'LightBulb' class.
What is a key benefit of having high-level modules depend on abstractions?
It simplifies the low-level modules.
It improves code readability.
It reduces the number of lines of code.
It makes the high-level module reusable with different implementations.
Consider a 'NotificationService' responsible for sending notifications. Which option violates DIP?
The 'NotificationService' accepts an 'INotificationChannel' in its constructor.
The 'NotificationService' directly instantiates and uses an 'EmailSender' class.
The 'NotificationService' depends on an 'INotificationChannel' interface.
Different channels like 'SMSChannel' implement the 'INotificationChannel' interface.
How can SOLID principles impact team collaboration in software development?
By promoting a shared understanding of code structure and design
By enforcing strict coding style guidelines
By automating code review processes
By reducing the need for communication within the team
In the context of ISP, why are 'role interfaces' considered good practice?
They violate the Single Responsibility Principle.
They define a broad set of methods used by many unrelated classes.
They are only applicable to abstract classes, not interfaces.
They are specific to how a particular client uses a class, regardless of its primary responsibility.
In the context of LSP, what is meant by 'substitutability'?
The ability to change the behavior of a superclass by modifying its subclass.
The idea that all methods in a subclass should be static.
The capability to use a subclass object wherever a superclass object is expected without causing issues.
The practice of always using abstract classes instead of concrete classes.
Consider a base class 'Bird' with a method 'fly()'. You create a subclass 'Penguin'. What would be the most LSP-compliant approach in this scenario?
Have 'Penguin' inherit 'fly()' and throw an exception, as penguins cannot fly.
Remove the 'fly()' method from the 'Bird' class entirely.
Rethink the inheritance hierarchy. 'Bird' might not be the right superclass for 'Penguin' if flying is a core characteristic.
Have 'Penguin' inherit 'fly()' but leave it empty, as penguins don't need to fly.
Why is the Open/Closed Principle important in software development?
It makes code less readable.
It promotes code duplication.
It increases the risk of introducing bugs during modification.
It complicates the development process.
How does DIP help make code more maintainable?
By reducing the number of classes needed
By making classes smaller
By making it easier to test modules in isolation
By reducing the need for comments
How does DIP relate to the concept of 'inversion of control'?
DIP and inversion of control are the same thing.
Inversion of control is a specific implementation of DIP.
They are unrelated concepts.
DIP is a specific implementation of inversion of control.