Imagine a class named 'Employee' that handles both employee data (like name, ID) and database operations. What SOLID principle does this violate?
Open/Closed Principle
Liskov Substitution Principle
Single Responsibility Principle
Interface Segregation Principle
A class EmailService implements an interface ICommunication with methods sendEmail(), sendSMS(), and makeCall(). Only the email functionality is relevant to EmailService. How can this design be improved according to ISP?
EmailService
ICommunication
sendEmail()
sendSMS()
makeCall()
Move all methods (sendEmail(), sendSMS(), makeCall()) to the EmailService class.
Throw an exception in EmailService for the unused methods: sendSMS() and makeCall().
Keep the current design, as it provides a single point of access for all communication methods.
Create separate interfaces: IEmailService, ISMSService, and ICallService, and have EmailService implement only IEmailService.
IEmailService
ISMSService
ICallService
Why is the Open/Closed Principle important in software development?
It complicates the development process.
It makes code less readable.
It promotes code duplication.
It increases the risk of introducing bugs during modification.
How can you identify multiple responsibilities within a class?
By checking if the class name is too long.
By counting the number of lines of code in the class.
By observing how many other classes it interacts with.
By looking for different reasons to change the class in the future.
How does DIP relate to the concept of 'inversion of control'?
DIP is a specific implementation of inversion of control.
They are unrelated concepts.
DIP and inversion of control are the same thing.
Inversion of control is a specific implementation of DIP.
How does using interfaces contribute to the Open/Closed Principle?
Interfaces define a contract that can be implemented by multiple classes, enabling flexibility.
Interfaces make code execution slower.
Interfaces force tight coupling between classes.
Interfaces allow for direct modification of existing class code.
Which of the following best describes the Dependency Inversion Principle (DIP)?
Both high-level and low-level modules should depend on abstractions.
High-level modules should depend on low-level modules.
Low-level modules should depend on high-level modules.
Abstractions should depend on concrete implementations.
How does DIP help make code more maintainable?
By making it easier to test modules in isolation
By reducing the need for comments
By reducing the number of classes needed
By making classes smaller
Why might strictly adhering to the Open/Closed Principle in every single part of a codebase be impractical?
It can lead to over-engineering and unnecessary complexity in some cases.
It violates other SOLID principles.
It simplifies the design too much.
It makes the codebase less secure.
Which of these is a benefit of adhering to the Single Responsibility Principle?
Increased class size for better code organization.
Tighter coupling between classes for better collaboration.
Reduced code duplication and improved reusability.
More complex class design for handling multiple tasks.