面向对象设计原则

2016-10-20  本文已影响0人  罟宁

第二章 面向对象的设计原则

在面向对象中,类是基本单位,各种设计都是围绕着类来进行的。类与类之间的关系,构成了设计模式的大部分内容

2.1 面向对象设计的五大原则

1、单一职责原则(Single Pesponsibility Principle,SRP)


    /**
    * 厨师类,命令接受者与执行者
    */
    class cook {
        public function meal() {
            echo "番茄炒蛋","<br/>";
        }
        public function drink() {
            echo "紫菜蛋花汤","<br/>";
        }
        public function ok() {
            echo "完毕","<br/>";
        }
    }
    /**
    * 命令接口(服务员)
    */
    interface Waiter {
        public function execute();
    }
    
    /**
    * 模拟服务员与厨师的交互过程
    */
    class MealWaiter implements Waiter {
        private $cook;
        public function __construct(cook $cook) {//类型限定,下同
            $this->cook = $cook;
        }
        public function execute() {
            $this->cook->meal();
        }
    }
    class DrinkWaiter implements Waiter {
        private $cook;
        public function __construct(cook $cook) {
            $this->cook = $cook;
        }
        public function execute() {
            $this->cook->drink();
        }
    }
    /**
    * 顾客点菜
    */
    class Custommers {
        private $mealWaiter;
        private $drinkWaiter;
        public function addWaiter(Waiter $mealWaiter, Waiter $drinkWaiter) {//类型限定
            $this->mealWaiter = $mealWaiter;
            $this->drinkWaiter = $drinkWaiter;
        }
        public function callmeal() {
            $this->mealWaiter->execute();
        }
        public function calldrink() {
            $this->drinkWaiter->execute();
        }
    }
    /**
    * 实现命令模式
    */
    $custommer = new Custommers;//一个顾客
    $cook = new cook;//一个厨师
    $mealWaiter = new MealWaiter($cook);//一个点菜的服务员
    $drinkWaiter = new DrinkWaiter($cook);//一个点汤的服务员
    $custommer->addWaiter($mealWaiter, $drinkWaiter);//叫来这两个服务员
    $custommer->callmeal();//点菜
    $custommer->calldrink();//点汤

2、接口隔离原则(Interface Segregation Principle, ISP)

3、开放-封闭原则 (Open-Close Principle, OCP)


    <?php
    //开放封闭原则的示例:播放器
    
    interface process {
        public function process();
    }
    //编码
    class playerencode implements process {
        public function process() {
            echo "encode","<br/>";
        }
    }
    //输出
    class playeroutput implements process {
        public function process() {
            echo "output","<br/>";
        }
    }
    
    //播放器的线程调度管理器
    class playprocess {
        private $message = NULL;
        public function __construct() {
    
        }
        public function callback(event $event) {
            $this->message = $event->click();
            if ($this->message instanceof process) {//instanceof:1、判断一个对象是否是某个类的实例;2、判断一个对象是否实现了某个接口
                $this->message->process();
            }
        }
    }
    
    //播放器的事件处理类,对事件进行分拣
    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;
            }
        }
    }
    
    //播放器的事件处理逻辑
    class mp4 {
        public function work() {
            $playProcess = new playProcess();
            $playProcess->callback(new event('encode'));
            $playProcess->callback(new event('output'));
        }
    }
    
    //实现
    $mp4 = new mp4();
    $mp4->work();

4、替换原则/里氏替换原则(Liskov Substitution Principle, LSP)

5、依赖倒置原则(Dependence Inversion Principle, DIP)/IOC(Inversion of Control)

上一篇 下一篇

猜你喜欢

热点阅读