6 design Principles (1): Single duty principle

Source: Internet
Author: User

Definition: You should and only have one reason to change the class.

Understand:

If a class takes on too much responsibility, it is tantamount to coupling these responsibilities, and a change in responsibility may weaken or inhibit the ability of this class to perform other duties,

This coupling can lead to fragile designs. When the change occurs, the design suffers unexpected damage.

If a class A, there are two responsibilities, a, a, when responsibility a due to changes in requirements and needs to be modified, it is possible to cause the function of Duty B to fail.

Solve:

Separate the responsibilities in the class, and create two classes respectively.

If it is interface-oriented programming, it is to abstract two responsibilities into two different interfaces, the original interface to implement both interfaces.

Benefits of a single responsibility principle:

1. Reduced complexity of classes

2. Improved readability

3. Improved maintainability

4. Reduced risk due to change

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

6 design Principles (1): Single duty 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.