Chapter 2 [object-oriented design principles]

Source: Internet
Author: User

(1) how to measure the quality of software design

Cohesion:

The number and diversity of tasks that a single unit of an application is responsible. Cohesion is related to a single class or a single method unit. (Good software design should be highly cohesive .)

Coupling Degree:

Coupling Degree indicates the closeness between classes. Low coupling means to use abstract coupling as much as possible, with less specific coupling.



Design Principle Name Introduction to design principles Importance
Single Responsibility Principle You can't put too many responsibilities in a class. ★★★★☆
Principle of opening/closing The software entity is open to the extension, but closed to the modification, that is, the function is extended without modifying a software entity. ★★★★★
Historical replacement principle In a software system, a subclass object must be accepted where the base class object can be accepted. ★★★★☆
Dependency reversal Principle Programming at the abstraction layer instead of specific classes. ★★★★★
Interface isolation principle Multiple special interfaces are used to replace a unified interface. ★★☆☆☆
Principles of combination/aggregation Reuse In the system, you should try to use combinations and aggregate associations as much as possible, and do not use inheritance relationships as little as possible. ★★★★☆
Dimit Law If a software entity references other entities less, the better, or if the two classes do not need to communicate with each other directly, the two classes should not interact directly, instead, an indirect interaction occurs by introducing a third party. ★★★☆☆



1) single responsibility principle, SRP ):

For a class, there should be only one reason for its change. Never let a class have multiple reasons for change. A class should only work on a task-related business, and should not put too many businesses in a class.


2) Open-Close principle (OCP ):

A software entity (class, module, method, etc.) should be open to the extension and closed to the modification. The principle of opening/closing is the core principle of design principles.


3) The liskov substitution principle, LSP ):

Subclass should be able to completely replace any place where the parent class can appear, and after replacement, the customer program that calls the parent class will not change in behavior.


4) Dependency inversion principle (DIP )):

High-level modules should not depend on low-level modules, both of which should depend on abstraction;

Abstraction should not depend on details, but on abstraction.

That is, for abstract programming, not for specific programming


5) Principles of combination/aggregation reuse (composite/aggregation Reuse Principle, CARP ):

It means to use combination/aggregation as much as possible to reuse code, and use less inheritance for reuse purposes.


6) interface segregation principle (ISP ):

It means that the customer should not rely on the methods they cannot use, but only provide the interfaces required by each customer. In other words, users cannot be forced to rely on interfaces they do not use. That is, it is better to use a special interface than to use a unified interface.


7) Law of Demeter (dump ):

A software entity should interact with other entities as little as possible. (Do not talk to strangers !)


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.