PHP design mode-Six principles ____php

Source: Internet
Author: User

Code that complies with the following six principles is generally considered to be extensible, maintainable, and highly reusable code:

1. Principle of single responsibility

Don't meddle.


2. The principle of open closure

Open to extension, closed for modification


3. Richter substitution principle

Subclasses can implement abstract methods of the parent class, but they cannot override non-abstract methods of the parent class. Subclasses can add their own unique methods. When a method of a subclass overloads a method of a parent class, the method's predecessor condition (that is, the method's formal parameter) is more relaxed than the input parameter of the parent class method. When a subclass's method implements an abstract method of the parent class, the method's post condition (the return value of the method) is stricter than the parent class.

A book from the p41 design pattern

"The Richter substitution principle is Barbara Liskov in 1988 published" ASD ", the specific mathematical definition is more complex, you can check the relevant information, its vernacular translation is a software entity if the use of a parent class, then must apply to its subclasses, And it does not observe the difference between the parent object and the subclass object, that is, in the software, the parent class is replaced by its subclass, the program behaves unchanged, and simply says that the subtype must be able to replace their parent type "ASD".

Richter Substitution principle (LSP): subtypes must be able to replace their parent types. "ASD"

"This seems to be the concept of learning to inherit, the subclass inherits the parent class, so subclasses can appear as parent classes." "

"Yes, I ask you a question, if in object-oriented design, a bird, a penguin class, if the bird can fly, the enterprise is not fly, then the enterprise is a bird." Can penguins inherit this kind of bird? ”

"Penguin is a special kind of bird, although it can't fly, but it is also a bird, of course can inherit." ”

"Ah, you fooled, I said in the face object design, that means what that." Subclasses have all non-private behavior and attributes of the parent class. Birds can fly, and penguins can't fly, although in the biological classification, Penguin is a bird, but in the programming world, penguins can not be the parent-bird identity, because the premise that all birds can fly, penguins can't fly, so, penguins can not inherit birds. ”
4. Interface Isolation principle


5. Reliance on reversal principles


6. Dimitri rule--at least know the principle


The six principles of any object-oriented language should be adhered to, good code is not necessarily in strict accordance with the design pattern of code written, if your code is consistent with these six principles, then your code is good code, so the six principles are still very important.



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.