Object-Oriented Design Principle-single responsibility principle-perception

Source: Internet
Author: User

1. Single Responsibility Principle

Class:

Ideally, a class should assume only one role to indicate one thing. We should not indicate that one thing is a and B. This is an ideal state, because in the actual process, considering a balance, there will inevitably be one class playing multiple roles. However, the single responsibility principle of the class should be the direction we are pursuing.

Method:

A method should only do a very clear thing, and the method name should be satisfactory.

Variable:

A variable should exist to record a value of a certain meaning.

Interface:

An interface is usually used to indicate that it has certain capabilities. We should ensure that behavior-related methods are encapsulated together, and each interface should have a single responsibility.

Disadvantages of not guaranteeing a single responsibility

1. Virtually added the number of codes in a class and the complexity of the Code.

2. The more factors involved, the higher the probability of being affected, and the higher the probability of modification. Because of the large class, the maintenance cost will be incurred. (We can have multiple classes. We only need to modify one class)

3. Chaotic design. The class plays multiple roles and has poor readability.

5. Serious code coupling and poor maintainability.

Swiss Army knife and single responsibility

There is a lot of conflict with our single responsibility principle. The Swiss Army Knife can provide a variety of knives and play multiple roles. The Swiss Army knife is a collection of knives, saving us the cost of carrying and purchasing other types of knives. We can use a Swiss Army knife to cut a watermelon, but if we have a real fruit knife, we still like it and are used to cutting it with a fruit knife.

Unlike a military knife that saves a lot of cost, it doesn't matter if we create multiple classes in the Code and create one class. Therefore, when creating classes and methods, there is no cost. It is best to create them based on roles and functions. We like fruit knives because they are professional, simple, easy to use and easy to operate. In fact, they are similar.

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.