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



























  • 相关阅读:
    78. Subsets
    93. Restore IP Addresses
    71. Simplify Path
    82. Remove Duplicates from Sorted List II
    95. Unique Binary Search Trees II
    96. Unique Binary Search Trees
    312. Burst Balloons
    程序员社交平台
    APP Store开发指南
    iOS框架搭建(MVC,自定义TabBar)--微博搭建为例
  • 原文地址:https://www.cnblogs.com/riskyer/p/3281500.html
Copyright © 2011-2022 走看看