What is the principle of a class has an only reason to change
Jump to turn (11)
- 1What is the principle of a class has an only reason to change
- 2Suppose I dev a clicker game. My class Enterprise changes each tick to earn mone
- 3Actually, the purchasing part of enterprises is dedicated to a mixin named Merch
- 4However, I concider making merch not a mixin, but a concrete class for compositi
- 5However I need a help. Merch class needs to call polymorphic reaction to success
- 6Be aware I use dart
- 7However now I am unsure if I follow the spirit of oop that is to model my classe
- 8The real world does not have a thing as fuelManager, as used your example. For m
- 9With that said, what should be the criteria for shaping a separate class instead
- 10SRP rewards creating a class with a single method
- 11Define a single reason to change
- User
What is the principle of a class has an only reason to change
- Assistant
The principle you are referring to is known as the "Single Responsibility Principle" (SRP). It is a principle of object-oriented design that states that a class should have only one reason to change.
According to the SRP, a class should have a single responsibility or a primary purpose. In other words, a class should have only one area of functionality that it is responsible for. This principle promotes the idea of high cohesion, where each class is focused on a specific responsibility or functionality.
By adhering to the SRP, a class becomes more modular, maintainable, and reusable. If a class has multiple responsibilities and undergoes changes related to each of those responsibilities, it can become more complex and harder to understand. When a change is required, it might affect other parts of the codebase that are unrelated, leading to potential bugs and increased maintenance efforts.