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();



























  • 相关阅读:
    MySQL Connection排查未授权用户(unauthenticated user)
    Git常用命令
    abc
    更新显卡驱动
    财务模块 采购、接收、应付会计分录和功能认识
    ebs界面颜色改变
    获取科目的描述 创建科目
    EBS总账(GL)模块常用表
    收款 付款 到总帐
    PO收料到入库的分析
  • 原文地址:https://www.cnblogs.com/riskyer/p/3281500.html
Copyright © 2011-2022 走看看