Single responsibility principle6/24/2023 ![]() ![]() We can follow the same principle for the Monitor class.Įxcellent! We've decoupled the dependencies and are free to test our Windows98Machine with whichever testing framework we choose. If we want, we can easily switch out the type of keyboard in our machine with a different implementation of the interface. To define a reason to change, we need to investigate what is the responsibility of our program. ![]() Well talk more about this definition later. Now our classes are decoupled and communicate through the Keyboard abstraction. For example, refactoring is not a reason to change in terms of the single responsibility principle. Gather together the things that change for the same reason and separate things that change for different reasons. The single responsibility principle is a design principle which states that every class or module should have one responsibility and one reason to change. Let's decouple our machine from the StandardKeyboard by adding a more general Keyboard interface and using this in our class: public interface Keyboard And we're stuck with our Monitor class too. Not only does this make our Windows98Computer hard to test, but we've also lost the ability to switch out our StandardKeyboard class with a different one should the need arise. The Single Responsibility Principle states that a software module should have a single responsibility, i.e., it should have one and only one reason to.
0 Comments
Leave a Reply. |