Question
Which of the following is a key principle of the SOLID
design principles that focuses on ensuring a class has only one reason to change?Solution
The Single Responsibility Principle (SRP) is one of the core SOLID principles in object-oriented programming. It states that a class should have only one reason to change, meaning it should only be responsible for one aspect of the softwareтАЩs functionality. This principle promotes high cohesion within a class and low coupling between classes, making the system more modular and easier to maintain. By adhering to SRP, developers can avoid the pitfalls of "God Classes," which handle multiple responsibilities and can lead to code that is difficult to debug, test, or extend. For example, a Report class that both formats and sends reports violates SRP, as changes to formatting or delivery methods would require modifying the same class. By separating these responsibilities into dedicated classes, such as ReportFormatter and ReportSender , the design becomes cleaner and easier to adapt to changes. Following SRP results in smaller, well-defined classes that are easier to understand, test, and maintain. It reduces the risk of introducing bugs when making changes, as a single modification is unlikely to impact unrelated parts of the application. Explanation of Incorrect Options: A) Open/Closed Principle : The Open/Closed Principle states that a class should be open for extension but closed for modification. While it ensures flexibility and stability, it focuses on adding new functionality without altering existing code. It does not emphasize the segregation of responsibilities within a class, making it incorrect in this context. B) Liskov Substitution Principle : The Liskov Substitution Principle ensures that derived classes can be substituted for their base classes without altering the correctness of the program. This principle addresses the behavior of subclasses but does not relate to a class having a single responsibility. C) Interface Segregation Principle : This principle advocates for creating small, specific interfaces instead of large, general ones. It ensures that classes implementing an interface are not burdened with methods they do not use. While important for interface design, it does not directly address the issue of a single reason for a class to change. E) Dependency Inversion Principle : The Dependency Inversion Principle emphasizes depending on abstractions rather than concrete implementations. It promotes flexibility and reduces tight coupling between high-level and low-level modules but does not ensure a single responsibility within a class.
PRIME RATE рдХреЗ рд▓рд┐рдП рдЕрдВрдЧреНрд░реЗрдЬреА рд╢рдмреНрдж┬а рд╣реИ тАУ
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдореЗрдВ рд╕реЗ рдлрд╕рд▓ рд╕рдореБрдЪреНрдЪрдп┬а рдХрд╛ рд╡рд┐рддреНрддреАрдп рд╢рдмреНрджрд╛рд╡рд▓реА рдХреЗ рдЕрдиреБ...
рдЬрдорд╛рдирдд рдкреНрд░рддрд┐рднреВрддрд┐ рдЬрдорд╛ рдХреЗ рд▓рд┐рдП рд▓рд┐рдП рд╕рд╣реА рдкрд╛рд░рд┐рднрд╛рд╖рд┐рдХ рд╢рдмреНрдж рд╣реИ
...рд▓реЗрдЦрд╛рдХрд╛рд░ рд╢рдмреНрдж рдХрд╛ рдЕрдВрдЧреНрд░реЗрдЬреА рдореЗрдВ рд╕рд╣реА рдЕрд░реНрде рд╣реИред
POLITICAL рдХреЗ рд▓рд┐рдП рд╕рд╣реА рд╣рд┐рдиреНрджреА рдкрд╛рд░рд┐рднрд╛рд╖рд┐рдХ рд╢рдмреНрдж рд╣реИ
рдЖрд╢рд╛рдмреНрджрд┐рдХ рд╕рдореНрдкреНрд░реЗрд╖рдг рдХрд╛ рд╡рд┐рд╢реЗрд╖ рд╢рд┐рдХреНрд╖рд╛ рдХреЗ рдХреНрд╖реЗрддреНрд░ рдореЗрдВ рдкреНя┐╜...
рд╡рдХреАрд▓реЛрдВ рдХреЛ рдиреНрдпрд╛рдпрдкрд╛рд▓рд┐рдХрд╛ рдХрд╛ рдореБрдЦрдкрддреНрд░ рд╣реЛрдирд╛ рдЪрд╛рд╣рд┐рдПред
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдореЗрдВ рд╕реЗ Adulterant рд╢рдмреНрдж рдХрд╛ рд╡рд┐рддреНрддреАрдп рд╢рдмреНрджрд╛рд╡рд▓реА рдореЗрдВ рд╕рд╣...
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдЕрдВрдЧреНрд░реЗрдЬреА рд╡рд╛рдХреНрдп рдХрд╛ рд╣рд┐рдВрджреА рдореЗрдВ┬а рд╕рд╣реА рдЕрдиреБрд╡рд╛рдж рдЪреБрдирд┐рдП...
(A ) рд╡рд┐рд╢реНрд╡ рдореЗрдВ рдХрдИ рджреЗрд╢ рдЕрдкрдиреА рд╕реНрд╡рд╛рд░реНрде рдкреВрд░реНрддрд┐┬а рдХреЗ рд▓рд┐рдП рдХреЛрдИ рднреА рдХрджрдо рдЙя┐╜...