(design mode 1) design pattern 5 major principles: solid principle

Source: Internet
Author: User
Tags abstract
Solid is an abbreviation for several important coding principles
Srp:the Single Responsibility Principle sole responsibility principle
Ocp:the Open Closed Principle opening and closing principle
lsp:the Liskov Substitution Principle Richter Replacement principle
Ld:the Law of Demeter Dimitri

Isp:the Interface Segregation Principle Interface separation principle
Dip:the Dependency Inversion Principle dependency inversion principle


One, the single principle of responsibility
There is only one reason to change the class. That is, each class does only one type of responsibility, and when the class has multiple responsibilities, the class is decomposed.


two, opening and closing principle
Open for extensions, close for modifications.
The open and closed principle is the most basic design principle in object-oriented design, and also the most vague principle of definition. The opening and closing principle can be summed up as follows: Using abstract component framework to realize the extension details.
The other 5 principles and the 23 design pattern are designed to follow the open and closed principle. The principle of single responsibility tells us that the realization of the class to be a single duty; the Richter substitution principle tells us not to break the inheritance system; The dependency inversion principle tells us to interface programming, and the interface isolation principle tells us to simplify the design of the interface, and the Dimitri Law tells us to reduce the coupling.



Three, the Richter replacement principle
Subclasses try not to overwrite the methods of the parent class.
Subclasses can extend the functionality of the parent class, but cannot change the original functionality of the parent class.
The ① subclass can implement an abstract method of the parent class, but cannot overwrite the non-abstract method of the parent class.
The unique method can be added to the ② subclass.
③ when a method of a subclass overloads a method of the parent class, the method's post condition (that is, the method's return value) is stricter than the parent class.

④ when a method of a subclass implements an abstract method of the parent class, the post condition of the method (that is, the return value of the method) is stricter than the parent class.


Four, the law of Dimitri

Also known as the least known principle, an object should keep a minimal understanding of other objects. Is that a class knows as little as possible about the classes it relies on.


Five, interface separation principle

Using multiple specialized interfaces is better than using a single total interface.

VI, dependency inversion principle

High-level modules should not be dependent on the underlying modules, which should all depend on abstraction. Abstractions should not be dependent on detail, and detail should be dependent on abstraction.

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.