How to define a class-single Responsibility Principle

Source: Internet
Author: User

Single Responsibility Principle: For a class, there should be only one reason for his change.

1. Example of a teacher Class

Or externally, a class should only be able to see its class-related functions. For example, teachers should only be responsible for teaching knowledge and preparing lessons, but should not be responsible for driving.

Practically speaking, a taskservice class should not contain the processing time class. It can be private, but it must not be public.
Another angle is introduced here.

2. What do you think of a class? 2.1 General Views

Generally, classes are data structures composed of member attributes and idiom methods.

Or the abstraction of real things.
For example, if a person is abstracted from a real person, the person has the basic attributes and behaviors of the person.

2.2 another angle

However, from another perspective, a class is a set of responsibilities. Class is responsible for the abstract responsibilities he represents.
For example, a teacher is a set of responsibilities such as Class, Class, teaching, and lesson preparation. He provides these interfaces to external users. The name age is usually private.

Examples of daily development
For example, a task class is a set of responsibilities such as viewing tasks, releasing tasks, recalling tasks, processing tasks, completing tasks, and disabling tasks.
The dotted line indicates the dependency, which can be considered as the use relationship. The Task Service class depends on the task entity class.

If there is a time processing method, this method must not be public. If this method is relatively independent, it can be encapsulated in the date class, introduce it through dependency.

3. High Cohesion and low coupling

When talking about a single responsibility, we often mention high cohesion and low coupling.
Cohesion is the closeness between operations in a class.
Low cohesion means that many functions are irrelevant. For example, the time processing function and the class function are different from each other. High Cohesion is closely related.
Coupling is the closeness between classes.
Or the degree of dependency. Low coupling means that the dependency between each other is small.
Low coupling: first, all classes are responsible for their own responsibilities, and different responsibilities are maintained by different classes. Therefore, the coupling between classes is low.
High Cohesion, a series of responsibilities are maintained by a single class, and all similar responsibilities are in the same class. This is high cohesion.
One-Class responsibilities should be concentrated in one class, rather than scattered in different classes, and different responsibilities should be accurately divided into different classes.
A negative example of High Cohesion and low coupling is an old project I am maintaining ..
Several parts of a function are divided into different places. SQL can see in JSP that there is no control layer, and a method is wrapped around multiple classes, most of the parts of this Division are not reusable.

We often say that some frameworks are heavy, so that the developer's classes and framework classes are coupled. the developer's classes must inherit a base class of the Framework. Once the base class has been upgraded, this may affect the entire system.

Q &

Q: I have a task class. It has a release task and a recall task. Is it a single responsibility?
A: Yes. Both belong to the job class.
Q: The single responsibility is only one cause of class changes. If there is a change in the release and recall tasks, isn't the category changed?
A: The reason is a series of responsibilities.
Q if the task class contains an open time processing interface, is this task Class A single responsibility?
A: No. The time does not belong to the task processing series. It should be set to private or put in dateutil.
Q What are the disadvantages of defining the time interface in the task processing class?
A:

  • First, time processing is not a task processing responsibility.
  • Second, the time processing interface is placed in the task processing class. Normally, all time processing interfaces are not placed in the task processing class, therefore, the responsibilities of time processing are dispersed in different classes, violating high cohesion. Due to the distribution of time processing interfaces in different classes, the responsibilities of these classes are coupled, violation of low coupling
  • When a third party calls the task processing class, it does not know why there is a time interface. It is not sure whether this method can be called or whether it will be changed in the future.
  • Once the time interface is changed, you need to consider whether it affects other interfaces in the task class. If the time interface is clustered in the time processing class, the relative impact will be small.
    If the examples using the task processing class and time interface are not obvious, we can immediately find the problem in the following example. They are essentially a problem, but the error degree is different:
    Define an interface for clearing system cache in the instructor class

How to define a class-single Responsibility Principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.