In the context of ISP, why are 'role interfaces' considered good practice?
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.
They define a broad set of methods used by many unrelated classes.
They violate the Single Responsibility Principle.
Imagine an interface IWorker with methods work() and takeBreak(). You have two classes, Robot and Human, both implementing IWorker. How would you refactor this to better align with the ISP?
IWorker
work()
takeBreak()
Robot
Human
Create two separate interfaces: IWorkable with work(), and IRest with takeBreak(). Implement them accordingly.
IWorkable
IRest
Have Robot implement IWorker, but leave Human without an interface.
Make takeBreak() an abstract method within IWorker so only Human has to implement it.
Keep the IWorker interface as is, as both classes can perform both actions.
What is the core idea behind the Single Responsibility Principle (SRP)?
A class should have a single, well-defined responsibility.
A class should be as small as possible.
A class should interact with as few other classes as possible.
A class should have only one method.
What is the core idea behind the Liskov Substitution Principle (LSP)?
Interfaces should be small and focused on a single task.
Code should be open for extension, but closed for modification.
Classes should have only one responsibility.
Subtypes should be substitutable for their base types without altering the correctness of the program.
Which of these is a benefit of adhering to the Single Responsibility Principle?
Tighter coupling between classes for better collaboration.
Increased class size for better code organization.
Reduced code duplication and improved reusability.
More complex class design for handling multiple tasks.
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?
Remove the 'fly()' method from the 'Bird' class entirely.
Have 'Penguin' inherit 'fly()' and throw an exception, as penguins cannot fly.
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.
How does the Open/Closed Principle contribute to creating maintainable code?
It allows for easier addition of new features without impacting existing ones.
It reduces the need for code documentation.
It makes the codebase smaller.
It eliminates the need for testing.
What is the core idea behind the Interface Segregation Principle (ISP)?
Code should be open for extension but closed for modification.
Clients should not be forced to depend on methods they don't use.
Subclasses should be substitutable for their base classes.
Imagine a class named 'Employee' that handles both employee data (like name, ID) and database operations. What SOLID principle does this violate?
Interface Segregation Principle
Open/Closed Principle
Liskov Substitution Principle
Single Responsibility Principle
In the context of the Open/Closed Principle, what does 'open for extension' mean?
A class's behavior should be modifiable through inheritance or polymorphism.
The source code of a class should always be accessible for any developer to modify.
Code should be heavily commented to explain every detail.
All classes should be loosely coupled and easily replaceable.