(8) Object-oriented design principle four

WBOY
Release: 2016-07-30 13:31:59
Original
829 people have browsed it

one. Replacement principles:
1. Subclasses must be replaceable with their base classes and appear anywhere where the parent class can appear.
2. Problems that need to be solved by the LSP principle:
a. How to design inheritance correctly
b. How to obtain the best inheritance hierarchy
c. Avoid the designed class hierarchy from falling into a situation that does not comply with OCP principles
3. Ways to comply with this principle:
a. All methods of the parent class must be implemented or rewritten in the subclass, and the derived class only implements the methods declared in its abstract class, but cannot give redundant method definitions or implementations.
b. Only parent class objects should be used in client programs and subclass objects should not be used directly. This enables runtime binding. (Dynamic polymorphism)

2. Example:

<?php
abstract class Cache{
    /*
     * 设置一个缓存变量
     *
     * @param String $key  缓存key
     * @param mixed $value 缓存内容
     * @param int $expire 缓存时间(秒)
     * return boolean 是否缓存成功
     */
    public abstract function set($key,$value,$expire=60);

    /*
     * 获得一个已经缓存的变量
     * @param String $key 缓存key
     * @return mixed 缓存内容
     */
    public abstract function get($key);

    /*
     * 删除一个已经缓存的变量
     * @return boolean 是否删除成功
     */
    public abstract function del($key);

    /*
     * 删除全部缓存变量
     * @return boolean 是否删除成功
     */
    public abstract function delAll($key);

    /*
     * 检测是否已经存在对应的缓存
     */
    public abstract function has($key);
}
Copy after login

Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the fourth object-oriented design principle (8), including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template