PHP Richter Replacement Principle Combat analysis

Source: Internet
Author: User
This time for everyone to bring the PHP Richter replacement principle of actual combat analysis, the PHP Richter replacement principle of the use of attention to what, the following is the actual case, take a look.

The replacement principle was put forward by Ms. Liskov of the MIT Computer Science Laboratory in an article at the OOPSLA conference in 1987, which mainly elaborated on some principles of succession, so called the principle of the Richter replacement.

In 2002, Robert C.martin published a book titled "Agile Software Development Principles Patterns and practices", in which he eventually reduced the principle of the Richter scale substitution to a sentence: " Subtypes must is substitutable for their base types "(subclasses must be able to be replaced with their base classes.) )

1. LSP Content

The definition and main ideas of the Richter replacement principle (Liskov Substitution Principle, LSP) are as follows: Because inheritance in object-oriented programming technology is too simple in specific programming, in many systems design and programming implementation, we do not seriously, Consider rationally whether the inheritance relationships between classes in the application system are appropriate, and whether derived classes can correctly override some of the methods in their base classes. As a result, there are often abuses of inheritance or the wrong inheritance, which brings a lot of trouble to the later maintenance of the system. This requires that we have a design principle to follow, which is the principle of substitution.

The LSP states that the subclass types must be able to replace their parent types and appear anywhere the parent class can appear. It guides us in how to properly inherit and derive, and to reuse code appropriately. This principle holds that if a software entity uses a base class, it must apply to its subclasses, and it is not aware of the difference between a base class object and a subclass object. Think about it, is it more like a polymorphic concept?

2. LSP is primarily for the design principles of inheritance

Because inheritance and derivation is one of the main features of OOP, it can reduce the repetitive programming implementation of code to realize the code reuse in the system, but how to carry out the inheritance design correctly and apply the inheritance mechanism rationally?

This is the problem that the LSP solves:

How to design inheritance correctly?

How is the best inheritance level obtained?

How to avoid the design of the class level into the situation of non-compliance with the OCP principle?

So how do you comply with the design principles?

1) The methods of the parent class are implemented or overridden in the subclass, and the derived class implements only the methods declared in its abstract class, and should not give redundant method definitions or implementations

2) in the client program, you should only use the parent class object instead of using the subclass object directly, which enables run-time binding (dynamic polymorphism).

If the A and B two classes violate the design of the LSP, it is common practice to create a new abstract class C, as a superclass of two specific classes, the common behavior of prizes A and B moved to C, thus solving the problem of inconsistent behavior of A and B.

However, PHP support for LSP is not good, the lack of upward transformation and other concepts, can only be achieved through a number of tortuous methods. For this principle, it is not to be said in detail here.

The following gives a cache implementation interface, using abstract classes to do the base class, follow the LSP implementation of its design.

<?phpabstract class cache{/**  * Set a cache variable  * @param $key cache key  * @param $value cache Content  * @param int $expire Cache time (seconds)  * @return Boolean is cached successfully *  /public abstract function set ($key, $value, $expire =);/**  * Gets a cached  * @param $key cache key  * @return Mixed Cache contents *  /public abstract function get ($key);/**  * Delete A variable that has already been cached  * @param $key Cache Key  * @return If Boolean is removed successfully *  /public abstract function del ($key);/**  * Delete all cache variables  * @r Eturn Boolean is removed successfully *  /public abstract function Delall ();/**  * Detects if there is a corresponding cache  * @param $key cache key  * @re Turn Boolean exists  */public abstract function has ($key);}

If you now want to implement caching under various mechanisms such as files, Memcache, Accelerator, and so on, simply inherit this abstract class and implement its abstract method.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

bootstrap+php implementation of multi-image upload steps

php+session prevent forms from repeating the steps to submit

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.