zoukankan      html  css  js  c++  java
  • 设计模式之策略模式

    <?php
    /**
     * 某个对象不必自身包含逻辑,而是能够应用其他对象的算法
     *
     */


    class LogStrategy
    {
        public $message;
        public $level;
        public $type;


        protected $_strategy;


        public function __construct($message, $level, $type)
        {
            $this->message = $message;
            $this->level = $level;
            $this->type = $type;
        }


        public function setStrategy($strategyObj)
        {
            $this->_strategy = $strategyObj;
        }


        public function log()
        {
            $this->_strategy->log($this);
        }
    }


    interface Log
    {
        public function log($logObject);
    }


    class DBStrategy implements Log
    {


        public function log($logObject)
        {
            $this->_logToDB($logObject);
        }


        private function _logToDB($logObject)
        {
            var_dump($logObject);
            echo "log to db ";
        }
    }


    class QUEStrategy implements Log
    {
        public function log($logObject)
        {
            $this->_logToQUE($logObject);
        }




        private function _logToQUE($logObject)
        {


            var_dump($logObject);
            echo "log to queue";
        }
    }


    echo "<pre>";
    $log = new LogStrategy("can't find the file", "error", "user_error");
    $log->setStrategy(new DBStrategy());
    $log->log();


    $log->setStrategy(new QUEStrategy());
    $log->log();



























  • 相关阅读:
    素因子分解
    【转载】一张表看懂LTE和5G NR的区别
    看国家宝藏,顺便学习一下国密算法
    LTE-Advanced(4G)主要技术学习:CA、CoMp、HetNet
    未来移动通信的需求与挑战
    傅里叶级数
    正余弦函数的复指数表示
    网络基础——相关面试考点
    操作系统——相关面试考点
    小米2015笔试编程题
  • 原文地址:https://www.cnblogs.com/riskyer/p/3281500.html
Copyright © 2011-2022 走看看