A look to understand the series of PHP design mode zero

Source: Internet
Author: User
Friendship Link:
1. A reading of the PHP design model of the series (i)
2. A reading of the PHP design model of the series (ii)

Objective

This article is I write three kinds of design patterns after writing, the reason, is because today get to some more design mode principle of things, whether it is the design mode or people usually write code, will inadvertently use what to abide by, I intend to use their words to write again.

You don't know the principle of design patterns

Single principle of responsibility

Defined

Do not have more than one cause for class changes. In layman's terms, a class is responsible for only one responsibility.

Vernacular understanding

Can work together as far as possible division of good workers, each responsible for their own piece, and do it well, to avoid mutual influence.

Principle of substitution on the Richter scale

Defined

All references to the base class must be able to use the object of its subclass transparently, that is, the subclass can extend the functionality of the parent class, but not the original function of the parent class.

Vernacular understanding

When you want to inherit the class written by the predecessor, try not to rewrite and reload the inside method, because you do not know, where will be affected. Inheritance methods also try to respect the original logic is not easily changed.

Dependency Inversion principle

Defined

High-level modules should not rely on the lower layers, both should rely on their abstraction; abstractions should not depend on detail; detail should be dependent on abstraction.

Vernacular understanding

The Father module does not depend on the sub-modules, it is best to rely on third parties (abstract class), can avoid high-coupling pits, in case of product change strategy, can also easily through scale-out (and then a subclass of inheriting abstract class) to solve, inherited words follow the Richter substitution principle

Interface Isolation principle

Defined

The client should not rely on interfaces it does not need, and the dependency of one class on another should be based on the smallest interface.

Vernacular understanding

If you are programming through the interface, do not put a bunch of abstract methods in the interface class, which will cause the inheriting class to implement the method that does not belong to it, even the interface, as far as possible, according to the concept of the total score, the common write in an interface class, individual custom should be written separately, subclass selective inheritance interface class is good.

Dimitri Law

Defined

An object should have a minimal understanding of other objects.

Vernacular understanding

Typical know too much more easy to die, put in the code is to write a module as far as possible to divide the points, a module contains more coupling, the more prone to error, split into separate modules, even if the problem will not collapse.

Opening and closing principle

Defined

A software entity such as classes, modules, and functions should be open to extensions and closed for modification.

Vernacular understanding

When product requirements change, try to implement changes by extending the behavior of the software entities (add class, add method) rather than modifying existing code to achieve change.

A discussion of a problem

很多优化的地方都在说:高内聚低耦合,那么问题来了,高内聚低耦合真的好吗?

My view is: Look at the specific logic of the business, because the high cohesion low coupling, once a piece of business interrelated and more complex, will directly lead to a lot of independent code blocks exist, even if there is a note, look at the code will give a jump to jump to the feeling that the reading will be reduced, directly resulting in maintainability will be reduced, when the code , posterity will spend a lot of time in understanding the code, unable to move. I heard that the appropriate coupling code and the specific dependency on the block more matching oh ~
It is not right to write code in accordance with the abstract norms summarized by the predecessors, and a good design pattern is a step-by-step change, not a design from the beginning.

Welcome message to Share

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above introduces a look at the PHP design pattern to understand the zero, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.