Seven Principles of object-oriented design ideas

Source: Internet
Author: User

When using the object-oriented idea for system design, our predecessors summarized seven principles:

1. Single Responsibility Principle (SRP)

The core idea of a single responsibility principle is that every object in the system should have only one independent responsibility, and all objects focus on the fulfillment of their own responsibilities. It is short for SRP and short for simple responsibility principle.

In fact, the single responsibility principle means that developers often say "high cohesion and low coupling ". That is to say, each class should have only one responsibility, and only one function can be provided externally, but there should be only one reason for class changes. All design patterns follow this principle.

2. Open and Close principle (OCP)

The core idea of the open/closed principle is that an object is open to extensions and closed to modifications. The abbreviation is OCP, and the full name is open for extension and closed for modification.

In fact, the open and closed principle means that the changes to the class are made by adding code, rather than modifying the existing code. That is to say, once software developers write code that can run, they should not change it. Instead, they must ensure that it can run continuously. How can they achieve this? This requires the use of abstraction and Polymorphism to abstract the content that may change, so that the abstract part is relatively stable, while the specific implementation layer can be changed and expanded.

3. Lean replacement principle (LSP)

The core idea of the Rys replacement principle is that its subclass can be used to replace any parent class. It is short for LSP and the full name is liskov substitution principle.

In fact, the principle of Lishi replacement means that objects in the same inheritance system should have common behavior characteristics. The Lee's replacement principle focuses on how to use inheritance well. That is to say, do not abuse inheritance. It is the cornerstone of inheritance reuse.

4. Dependency injection principle (DIP)

The core idea of the dependency injection principle is: to rely on abstraction, not specific implementation. It is short for dip and full for dependence inversion principle.

In fact, the dependency injection principle means that in an application, all classes should depend on the abstract classes of these other classes if they are used or dependent on other classes, instead of the specific implementation classes of these other classes. Abstract layers should not depend on specific implementation details, so as to ensure the reusability and maintainability of the system. To implement this principle, developers are required to program the interface rather than the implementation programming during programming.

5. Interface separation principle (ISP)

The core idea of the interface separation principle is: Customers should not be forced to rely on methods they do not need to use. It is short for ISP and full for interface segregation principle.

In fact, the interface separation principle means that an interface does not need to provide too many behaviors. An interface should only provide one external function, all operations should not be encapsulated into an interface.

6. dumit principles)

The core idea of the dimit principle is that an object should have as little understanding as possible about other objects. It is short for "slice" and the full name is "law of Demeter.

In fact, the dimit principle means to reduce the coupling between objects and improve the maintainability of the system. The communication between modules should only be through interfaces, regardless of the internal working principle of the module, which can minimize the Coupling Degree of each module and promote software reuse.

7. prioritize the use of combinations rather than inheritance principles (CARP)

The core idea of the principle of preference over combination rather than inheritance is to give priority to combination, rather than inheritance. Its abbreviation is Carp, and its full name is composite/aggregate Reuse Principle.

In fact, the principle of combination rather than inheritance is to prioritize the use of combination rather than inheritance when reusing objects. This is because when inheritance is used, any change to the parent class may affect the behavior of the Child class. When using a combination, it is dynamically defined at runtime by obtaining reference to other objects, this helps maintain a single responsibility principle for each category.

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.