Simple object-oriented principle)

Source: Internet
Author: User
I. "open-closed" principle (OCP)
The principle of open-closed principle is that a software entity should be open to extensions and closed to modifications.

Advantages:
By extending existing software systems, new behaviors can be provided to meet new requirements for the software, so that the software in change has certain adaptability and flexibility.
Existing software modules, especially the most important abstract layer modules, cannot be modified, which makes the software system in change stable and continuous.

Ii. Li's replacement principle (LSP)

Liskov substitution principle (Rishi replacement principle): Subtypes must be able to replace their base types.

Iii. Dependency inversion principle (DIP)

Dependence inversion principle: It depends on abstraction and not on specifics.

In short, the dependency inversion principle requires the client to rely on abstract coupling. Principles:

Abstraction should not depend on details; details should depend on abstraction;
Programming for interfaces is not implemented.
 
Use traditional proceduralProgramThe dependency created by the design depends on details, which is poor because the policy is affected by changes in details. The dependency inversion principle makes the details and policies depend on abstraction. The stability of abstraction determines the stability of the system.

Iv. Interface isolation principle (ISP)

Interface segregation principle: it is better to use multiple special interfaces than to use a single total interface. In other words, from the perspective of a customer class: the dependence of a class on another class should be based on the minimum interface.

An interface that is too bloated is a pollution to the interface. Customers should not be forced to rely on methods they do not need.

Implementation Method:
1. Use the delegate separation Interface
2. Use multiple inheritance and separation Interfaces

5. Principles of synthesis/aggregation multiplexing (CARP)

Composite/aggregate Reuse Principle or carp is often referred to as Composite Reuse Principle or CRP ), it is to use some existing objects in a new object to make it a part of the new object. New objects reuse existing functions by delegating these objects.

In short, we should try to use synthesis/aggregation instead of inheritance.

Distinguish between "has-a" and "is-"

"Is-a" is strictly defined in the taxonomy meaning that one class is another class "one ". "Has-a" is different. It indicates that a role has a certain responsibility.

One common cause of incorrect inheritance rather than merging/aggregation is that "has-a" is treated as "is-".

For example:
 
In fact, employees, managers, and students describe a role. For example, if a person is a "manager", it must be "employee", and another person may be "Student employee". In the above design, A person cannot have multiple roles at the same time. If he is an "employee", he cannot be a "student". This is obviously unreasonable.

The error is caused by confusion between the "role" level structure and the "person" level structure, and mistaken "has-a" as "is-". Solution:
 

Vi. dumit rules)

The law of Demeter (or the short-cut level of measure) is also called the least knowledge principle (least knowledge principle or LKP). That is to say, an object should have as little knowledge as possible about other objects.

Other statements:
Only communicate with your friends
Do not talk to "Strangers"
Each software organization has only the minimum knowledge of other units and is limited to software units closely related to the same unit.

Dimit rules and Design Patterns
Facade mode and mediator Mode

Make people ignorant
The third chapter of Lao Tzu said: "It is based on the governance of saints, and lacks its heart, its belly, and its ambition. It often makes the people ignorant and heartless. The "Ignorance" of the "ruling" object is in the "Ignorance" state, which can reduce the cost of "ruling.
The so-called "Minimum knowledge" principle is actually the rule of Lao Tzu's "ignorance of the people.

Transactions
Lao Tzu cloud: "little country, few people ...... Neighboring countries look at each other and hear each other from each other. "Isolate the ruled objects so that they do not have direct communication, which can achieve the effect of splitting and then divide and conquer them separately. The dimit rule coincides with Lao Tzu's "little country and few people" rule.

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.