Single Responsibility principle _ Design pattern

Source: Internet
Author: User
1 function It instructs us how to improve the reusability of the code! 2 definition

A class should have only one reason to cause it to change (responsibility), or a class is responsible for only one functional area of the corresponding responsibility. This principle is also called the "high cohesion principle" of class design.

(l) One of the meanings: avoid the same responsibilities (also called functions) dispersed into different classes.

(2) The meaning of the second: should also avoid a class to assume too much responsibility.

Coupling too much responsibility into a class leads to a fragile design.

A lot of what software design really does is to identify responsibilities and separate those responsibilities from each other. 3 analysis of the principle of single responsibility

1 The more responsibilities a class (or large to a module, small to a method) undertakes, the less likely it is to be reused. And if a class takes on too much responsibility, it is equivalent to coupling these responsibilities together, which may affect the operation of other responsibilities when one of the responsibilities changes.

2 The duties of the class mainly include two aspects: data responsibility and behavior responsibility, the data responsibility is embodied by its attribute, and the behavior responsibility is embodied by its method.

3 The principle of a single responsibility is to achieve high cohesion, low coupling guidelines, in many code refactoring methods can find its existence, it is the simplest but most difficult to apply the principle, need designers to discover the different responsibilities of the class and separate it, The multiple responsibilities of the discovery class require the designers to have strong analytical design ability and related reconstruction experience. 4 Why to observe the principle of sole responsibility.

Can reduce the coupling between classes

When the requirements change, only one class is modified, thus isolating the change;

If a class takes on too much responsibility, it is tantamount to coupling these responsibilities together, and a change in responsibility may weaken or inhibit the ability of the class to perform other duties. This coupling can lead to fragile design, and when the change occurs, the design will suffer unexpected damage. 5 Consequences of Non-compliance with the principle of sole responsibility

Will affect the reuse of the class.

When it is necessary to reuse only one of the duties of the class, it is difficult to isolate it as it is coupled with other responsibilities.

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.