OO design principles

Source: Internet
Author: User

Design principles are basic tools. applying these rules can enable yourCodeIt is more flexible, easier to maintain, and easier to expand.

Basic Principles (OO design princples)

Closed change encapsulate what varies.

Code to an interface rather than to an implementation

Combination is preferred, rather than favor composition over inheritance

SRP: the single responsibility principle single responsibility

In the system, each object should have only one independent responsibility, and all objects are concerned with the completion of their own responsibilities.

Every object in your system shocould have a single responsibility, and all the objects services shocould be focused on carrying out that single responsibility.

1. every responsibility is a design change. When the demand changes, the demand changes are reflected in the changes of class responsibilities. When the objects in your system have only one reason for the change, you have followed the SRP principle.

2. If a class has too many responsibilities, these responsibilities are coupled.

3. SRP makes the system easier to manage and maintain because not all problems are involved.

4. Cohesion cohesion is actually another name of the SRP principle. If you write high cohesion software, it means that you have applied the SRP principle well.

Dry: Don't repeat yourself Principle

By extracting public parts and placing them in one place, you can avoid code duplication.

OCP: Open-Close principle Principle

Class should be closed for modification and open for extension;

Classes shoshould be open for extension, and closed for Modification

LSP: The liskov substitution principle

The subclass must be able to replace the base class

Subtype must be substitutable for their base types.

1. LSP focuses on how to use inheritance well.

2. You must know whether to use a method or extend it, but it is definitely not changed.

3. LSP clearly pointed out that the IS-A relationship of OOD is in terms of behavior patterns, the behavior patterns can be reasonable assumptions. YescustomerProgram.

4. LSP makes us come to an important conclusion: If a model is isolated, it is not really valid unilaterally.

5. Violation of LSP may also violate OCP.

Dip: Dependency inversion principle.

High-level modules should not depend on the underlying layer, and both of them should depend on abstraction.

Abstraction should not depend on details, but on abstraction.

ISP: interface isolation principle

Customer programs should not be forced to rely on methods they do not need.

Why are we too involved in details? What is the nature of the problem?

There is no layer of thinking. This is the essence of the problem.

A kind person is confused in his pursuit, but will realize that there is a proper path. Fu Shide

Three-layer perspective: conceptual perspective, normative perspective, and implementation perspective. The perspective helps us divide and isolate problems.

Related Article

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.