(2) single responsibility principle and (3) open-closed Principle

Source: Internet
Author: User

Single Responsibility Principle SRP: Singleresponsibility principle. For a class, there should be only one reason for its change.

If a class has too many responsibilities, it is equivalent to coupling these responsibilities together. A change in responsibilities may weaken or restrain the class from fulfilling other responsibilities. This coupling will lead to a fragile design. When a change occurs, the design will suffer unexpected damages. Just as a person may assume many important responsibilities, it is especially important for him to allocate time to perform various tasks. If two or more things are solved by one person at a time, then the effect of doing anything will be affected. Therefore, GTD is required.

What software design really needs to do is to discover responsibilities and separate them from each other. In fact, it is necessary to determine whether classes should be separated. If you can think of more than one motive to change a class, then this class has more than one responsibility. Of course, GTD does not want to separate multiple self, but to allocate multiple time periods, with only one me in each time period. In this way, the effect of one class and one responsibility is achieved.

In programming, we need to think more about the separation of duties, so that the code is really easy to maintain, easy to expand, easy to reuse, flexible and diverse.

Open-Close principle OCP:Theopen-closed
Principle

In the software design model, the idea that cannot be modified but can be expanded is the most important design principle, that is, the open-closed principle: software entities (classes, modules, functions, etc) it should be scalable, but not modifiable. In order to remain relatively stable in the face of changes in demand, so that the system will continuously launch new versions after the first version, it is necessary to open the extension and close the change. In the example of a calculator, we add an abstract computing class and isolate the coupling between a specific algorithm and the client through some object-oriented methods such as inheritance and polymorphism, to add other operations such as root operations, you do not need to change the existing algorithm class and client, but add a subclass. Therefore, changes to the program are made by adding new Code, rather than modifying the existing Code. This is the spirit of the open-closed principle. Therefore, no matter how closed the module is, there will be some changes that cannot be closed to it. Since it cannot be completely closed, the designer must select which changes should be closed for the module, guess the most likely types of changes, and construct an abstraction to isolate those changes. However, we should only abstract those parts that frequently change in the program, and do not deliberately abstract each part.

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.