美文网首页
计算器——简单工厂模式

计算器——简单工厂模式

作者: 禾线子 | 来源:发表于2017-07-25 22:14 被阅读0次

    1.目录结构

    目录结构.png

    2.工厂模式解析

    待补充

    3.计算基类

    class BaseOperator
    {
        private $num_1    =  null;
        private $num_2    =  null;
    
        public function setFirstNum($num)
        {
            $this->num_1   =  $num;
        }
    
        public function setSecondNum($num)
        {
            $this->num_2   =  $num;
        }
    
        public function getFirstNum()
        {
            return $this->num_1;
        }
    
        public function getSecondNum()
        {
            return $this->num_2;
        }
    }
    

    4.计算接口类

    interface InterfaceOperator
    {
        function doOperate();
    }
    

    5.计算类

    include_once './BaseOperator.php';
    include_once './InterfaceOperator.php';
    
    class ActionAdd extends BaseOperator implements InterfaceOperator
    {
        // 加法
        public function doOperate()
        {
            $result   =  null;
            $num_1    =  parent::getFirstNum();
            $num_2    =  parent::getSecondNum();
            $result   =  $num_1 + $num_2;
            return $result;
        }
    }
    
    class ActionSub extends BaseOperator implements InterfaceOperator
    {
        // 减法
        public function doOperate()
        {
            $result    =  null;
            $num_1     =  parent::getFirstNum();
            $num_2     =  parent::getSecondNum();
            $result    =  $num_1 - $num_2;
            return $result;
        }
    }
    
    class ActionDiv extends BaseOperator implements InterfaceOperator
    {
        // 除法
        public function doOperate()
        {
            $result    =  null;
            $num_1     =  parent::getFirstNum();
            $num_2     =  parent::getSecondNum();
            if ($num_2 == 0) {
                throw new \Exception('Number invalid', '2202');
            }
            $result    =  $num_1 / $num_2;
            return $result;
        }
    }
    
    class ActionMul extends BaseOperator implements InterfaceOperator
    {
        // 乘法
        public function doOperate()
        {
            $result    =  null;
            $num_1     =  parent::getFirstNum();
            $num_2     =  parent::getSecondNum();
            $result    =  $num_1 * $num_2;
            return $result;
        }
    }
    

    6.计算工厂类

    include_once './FunctionOperator.php';
    
    class FactoryOperator
    {
    
        /**
         * @param $operation
         * @return ActionAdd|ActionDiv|ActionMul|ActionSub
         * @throws Exception
         */
        public static function factory($operation)
        {
            switch($operation)
            {
                case "+":
                    $operator    =  new ActionAdd();
                    break;
                case "-":
                    $operator    =  new ActionSub();
                    break;
                case "*":
                    $operator    =  new ActionMul();
                    break;
                case "/":
                    $operator    =  new ActionDiv();
                    break;
                default:
                    throw new \Exception('Operation invalid', '2202');
                    break;
            }
            return $operator;
        }
    }
    

    6.模拟

    include_once './FactoryOperator.php';
    try {
        $operator    =  FactoryOperator::factory('*');
        $operator->setFirstNum(4);
        $operator->setSecondNum(2);
        $result      =  $operator->doOperate();
        echo "Result is $result";
    } catch (\Exception $e) {
        echo $e->getTraceAsString();
    }
    
    效果.png

    7.感想

    通过分层达到了解耦的目的。感觉越高级的东西就有越多的分层,在分层的道路上越走越远。
    工厂类的左右是实例化一个对象,也仅仅是实例化一个对象。其他属性不要在工厂类里面进行设置,而是在实例化之后进行设置(setFirstNum...)。

    相关文章

      网友评论

          本文标题:计算器——简单工厂模式

          本文链接:https://www.haomeiwen.com/subject/opbgkxtx.html