The root problem was the assumption that all cats scratch, so fix that problem at the source, rather than just bandaging it at the destination. Create a Claws class and contain that within the Cats class. their structure - attributes & behavior - are specific to that single functionality only to which the class caters to. The place to fix this problem is not in the base class, but in the original Cat class. And only when there is a change needed in that specific task or functionality should this class be changed.Īlso note that the classes defined using the Single Responsibility Principle are inherently cohesive in nature, i.e. Trong cc ngn ng lp trnh vi kiu d liu tnh nh C++, Java, mt trong nhng yu t h tr cho tru tng v a hnh. Nguyn l ca OCP da trn hai thnh phn chnh l tru tng (abstraction) v a hnh (polymorphism). What the definition means - When we design our classes, we should take care that one class at the most is responsible for doing one task or functionality among the whole set of responsibilities that it has. Nguyn tc thay th Liskov - Liskov Substitution Principle. Now, combining the two definitions of Single Responsibility Principle based on how we defined Responsibility we get the final definition as below -ĭefinition of Single Responsibility Principle A class should have a single responsibility, where a responsibility is nothing but a reason to change. →A Responsibility is nothing but a reason to change. For this we have to understand what is meant by Responsibility in the context of Single Responsibility Principle. → A class should have only one responsibility.īoth of these definitions actually mean the same. Wikipedia and many write-ups on Single Responsibility Principle describe it as. → A class should have only one reason to change. Single Responsibility Principle was defined by Robert C. Specifically, the S in SOLID stands for Single Responsibility Principle. Single Responsibility Principle is one of the five principles of SOLID Design Principles. This article explains Single Responsibility Principle with Example in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |