Design Pattern Note 3: Design Patterns several principles

Source: Internet
Author: User

1. Single Responsibility Principle

Definition: As far as a class is concerned, there should be only one cause for his change.

If a class has too much responsibility, it is tantamount to coupling those responsibilities together. A change in responsibility may weaken or inhibit the ability of this class to perform other duties. This coupling leads to fragile designs that can be subjected to unexpected damage when the changes occur.

The real thing about software design is to discover responsibilities and separate responsibilities, and if you can think of an extra motive to change a class, then this class has a redundant responsibility.

2. Open-Close principle

Software entities can be extended, but cannot be modified.

  When you first write your code, assume that the change does not occur. When changes occur, we create abstractions to isolate changes that may occur in the future.

In the face of demand, changes to the program are made by adding new code, rather than changing the existing code.

Rejecting immature abstractions is as important as abstraction itself.

3. Dependency reversal principle

1, high-level modules and the underlying module should be dependent on abstraction.

2, detail also relies on abstraction.

4. The principle of substitution on the Richter scale

Subclasses can replace parent types.

Only if the subclass can replace the parent class, the Software unit function is not affected, the parent class is actually reused, and the subclass can also be a new behavior on the basis of the parent class.

Because of the substitution of subtypes, modules that use the parent class type can be extended without modification.

All dependencies in a program are terminated by an abstract class or interface, which is an object-oriented design.

Design Pattern Note 3: Design Patterns several 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.