Seven programmers ' object-oriented design principles

Source: Internet
Author: User

I'm just a bean before I know the 7 principles of object-oriented design! But the beans will eventually grow, don't they?

1. Opening and closing principle : A software entity should open to the extension and close the modification. In other words, when a module is involved, it should be allowed to be extended without modification, that is, to change the behavior of the module without modifying the source code.

   In the definition of the open and closed principle, a software entity can refer to a software module, a local structure consisting of multiple classes, or a class that reads a library.

   abstraction is the key to the open and closed principle.

is to add a refactoring of the new code completion method instead of modifying the source code

Disclaimer: This article originates from

2. Dependency reversal principle : High-level modules should not rely on low-layer modules, they should all rely on abstraction. Abstractions should not be dependent on detail, and detail should be dependent on abstraction.

To program for the interface, do not program for implementation.
The code relies on abstract classes rather than on specific classes, programming for interfaces or abstract classes, rather than specific classes.
        the key to realize the open and close principle is abstraction, and from abstract to materialize realization, if the principle of open-end is object-oriented design, then the dependency reversal principle is oriented towards
Like the main means of design.
One of the common implementations of the dependency reversal principle is the use of abstract classes in code . Instead, the specific class is placed in the configuration file .
Dependency Injection: construct injection: Inject the instance variable by constructor : Inject the visual variable through the Setter method : Inject the instance variable by the interface method

Disclaimer: This article originates from

3. Richter Replacement principle : Subclasses appear where the parent class appears, without affecting the program's operation. But in turn, the parent class object cannot replace the subclass object.

With the Richter scale substitution principle, it is possible to reuse the inheritance,

Subclasses can add new behaviors based on the parent class. (subclasses are better than the parent class)

The subtype must be able to replace his parent type .

Pass: Learn More

4. Single Responsibility Principle : An object should contain only a single responsibility , and that responsibility is encapsulated in a specific class .

That is, a class only do a single thing , to avoid the emergence of miscellaneous classes, so that the class of inheritance and overloading more convenient.

coupling (coupling), also known as coupling degree, is a measure of the degree of association between modules. The strength of the coupling depends on the complexity of the interface between the modules, the way the module is called, and the interface
The amount of data. The coupling degree between modules refers to the dependencies between modules, including control relationships, call relationships, and data transfer relationships. The more connections between modules, the stronger the coupling, and
Indicate that their independence is worse. in software design, coupling degree and cohesion are usually used as the standard to measure the independence degree of the module. One of the criteria for partitioning a module is cohesion-poly-low coupling.

Pass: Learn More

5. Interface Isolation principle : Clients should not rely on interfaces that he does not need.

When using an interface, provide the client with the specified interface size, on demand, to hide interfaces that the client does not need.

The principle of interface isolation is that you do not use a single total interface, and each interface should assume a relatively independent role.

The class should rely entirely on the corresponding dedicated interface.

Pass: Learn More

6. Synthetic multiplexing Principle: Use object combinations as much as possible, rather than using inheritance to achieve reuse.

In general, when using inheritance , strictly abide by the Richter replacement principle, the base class details for the subclass is visible, this method is static,

reduces the flexibility of the program,       If the base class changes, its subclasses also have to change.

In general, first use " composition/Aggregation " to reduce the class-to-class coupling degree, reducing The effect of class changes on other classes.

The principle of synthetic multiplexing is to use some existing objects in a new object to make them part of the new object through association relationships (including combinatorial relationships and aggregation relationships), and the new object to invoke the method of the existing object to achieve the purpose of multiplexing. In short: When you reuse, use the combination/aggregation Relationship (association relationship) sparingly and inherit less .

Pass Learn More

7. Dimitri Rule : A software entity interacts with as few as possible with other software entities.

Reduces the direct interaction between objects and objects, creates intermediate classes, and uses intermediate classes to interact indirectly between objects and objects.

When we need to add or remove space, we just have to make adjustments in the middle class without modifying the source code of the existing control.

Reduce the degree of coupling between programs, so that classes are loosely coupled between classes. The program is more flexible.

Flexible use of object-oriented guidelines makes our programming more flexible, better packaged, more reusable, and easier to maintain.

Seven programmers ' object-oriented design principles

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.