One of the five principles of object-oriented: Single responsibility principle (self-understanding)

Source: Internet
Author: User

Http://www.cnblogs.com/seacryfly/archive/2011/12/29/2305965.html

Only changes to the (unique) responsibility (requirement) of the class will cause the refactoring of the code.

The single responsibility principle states that every module or class should has responsibility over a single PA RT of the functionality provided by the software, and that responsibility should is entirely encapsulated by the class. All its services should is narrowly aligned with the that responsibility. Robert C. Martin expresses the principle as follows:[1]

A class should has only one reason to change.

2.2 Single Duty principle

2.2.1 Introduction

A good system design, emphasizing the relationship between the module to maintain low coupling, high cohesion, in object-oriented design This rule also applies, so the object-oriented first design principle is: single responsibility principle (Srp,single Responsibility Principle).

A single duty, emphasizing the separation of duties, to a certain extent, the understanding of responsibilities, constitutes the key to the design of the coupling between different classes, so the single responsibility principle is more or less a basic principle that must be considered in the design process.

2.2.2 Classics

With regard to the principle of single responsibility, the core idea is:

A class, it is best to do only one thing, only a cause of its change.

The principle of single responsibility can be regarded as the extension of low-coupling, cohesion-poly in object-oriented principle, and defines responsibility as the cause of change, in order to improve cohesion to reduce the cause of change. The more responsibilities that may cause it to change, the more it will lead to a dependency on responsibility, which has an impact on each other, thus greatly damaging the cohesion and coupling degree. A single responsibility usually implies a single function, so do not implement too many functional points for the class to ensure that the entity has only one cause for it to change.

Therefore, the core of the SRP principle is that a change to a class can only be one, for a class that violates this principle should be refactored, such as façade or proxy mode separation of responsibilities, through the basic method Extract Interface, Extract The class and extract method are combed.

One of the five principles of object-oriented: Single responsibility principle (self-understanding)

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.