Home  >  Article  >  Backend Development  >  (7) Object-oriented design principle three

(7) Object-oriented design principle three

WBOY
WBOYOriginal
2016-07-30 13:32:00936browse

one. Open and closed principles:
The basic idea is:
1. Open: The behavior of the module must be open, support expansion, and not be difficult to change.
2.Closed: When extending the functions of the module, the original program module should not be affected or affected on a large scale.

2. Example:

<?php
interface process{
    public function process();
}

//播放器的编码
class playerEncode implements process{
    public function process(){
        echo "encode\r\n";
    }
}

class playerOutput implements process{
    public function process(){
        echo "output\r\n";
    }
}

//调度管理器
class playProcess{
    private $message = null;
    public function __construct(){

    }
    public function callBack(event $event){
        $this->message= $event->click();
        if ($this->message instanceof process){
            $this->message->process();
        }
    }
}

//播放器的事件处理
class mp4{
    public function work(){
        $playProcess = new playProcess();
        $playProcess->callBack(new event('encode'));
        $playProcess->callBack(new event('output'));
    }
}

//事件处理类
class event{
    private $m;

    public function __construct($me){
        $this->m = $me;
    }

    public function click(){
        switch($this->m){
            case 'encode':
                return new playerEncode();
                break;
            case 'output':
                return new playerOutput();
                break;
        }
    }
}

$mp4 = new mp4();
$mp4->work();
three. Others:
1. Fully apply the ideas of "abstraction" and "encapsulation" in design.
a. Find the "variable factors" and encapsulate them.
b. The “same” variable factors should be encapsulated into the “same” object.
2. Apply interface-oriented programming in the implementation of system function programming.
a. When requirements change, new classes can be provided to adapt to changes.
b. Interface-oriented requires functional classes to implement interfaces and objects to be declared as interface types.

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 third principle of (7) object-oriented design, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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