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
Keep the IWorker interface as is, as both classes can perform both actions.
Have Robot implement IWorker, but leave Human without an interface.
Create two separate interfaces: IWorkable with work(), and IRest with takeBreak(). Implement them accordingly.
IWorkable
IRest
Make takeBreak() an abstract method within IWorker so only Human has to implement it.
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()' but leave it empty, as penguins don't need to fly.
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.
How does the Interface Segregation Principle contribute to loose coupling in software design?
By reducing the need for unit testing.
By encouraging the use of global variables for communication between classes.
By promoting the use of concrete classes instead of interfaces.
By minimizing dependencies between classes to only what is absolutely necessary.
In the context of DIP, what are abstractions typically represented by?
Concrete classes
Database connections
Interfaces or abstract classes
User interface components
In the context of SRP, what does 'reason to change' mean?
Any modification made to the class code.
A change in the application's requirements related to a specific functionality.
Refactoring the class for improved readability.
Adding new features unrelated to the class's current responsibility.
What is the primary purpose of the Open/Closed Principle?
To ensure all methods are open for modification
To allow extending functionality without altering existing code
To enforce the use of abstract classes
To prevent modification of existing code
Which of these is NOT a valid approach to refactor a class violating SRP?
Delegate responsibilities to other existing classes.
Use design patterns like Strategy or Template Method to separate concerns.
Extract separate functionalities into new classes.
Combine all the responsibilities into a single method for better cohesion.
Which of the following scenarios most likely violates the Liskov Substitution Principle?
A subclass has more attributes than its superclass.
A subclass implements an interface that the superclass does not implement.
A subclass throws an exception that is not declared in the superclass's method signature.
A subclass overrides a method of its superclass and provides additional functionality.
How does the Open/Closed Principle contribute to creating maintainable code?
It reduces the need for code documentation.
It allows for easier addition of new features without impacting existing ones.
It makes the codebase smaller.
It eliminates the need for testing.
How does the Interface Segregation Principle relate to the idea of 'programming to an interface, not an implementation'?
ISP only applies to statically typed languages, while 'programming to an interface' is a dynamic language concept.
ISP contradicts this idea by promoting the use of concrete classes.
ISP helps achieve the flexibility of using different implementations by keeping interfaces minimal and focused on client needs.
They are unrelated concepts in software design.