zoukankan      html  css  js  c++  java
  • PHP工厂模式demo

    <?php
    //工厂模式

    interface operstion{
    function oper($a,$b);
    }
    //加
    class add implements operstion{
    function oper($a,$b){
    echo $c = $a + $b;
    }
    }
    //减
    class subtract implements operstion{
    function oper($a,$b){
    echo $d = $b - $a;
    }
    }
    //乘
    class multiply implements operstion{
    function oper($a,$b){
    echo $e = $a * $b;
    }
    }
    //除
    class divide implements operstion{
    function oper($a,$b){
    $f = $b / $a;
    }
    }
    class Factory{
    static public function createDb($type){
    switch($type){
    case 'add':
    return new add();
    break;
    case 'subtract':
    return new subtract();
    break;
    case 'multiply':
    return new multiply();
    break;
    case 'divide':
    return new divide();
    break;
    }
    }
    }

    $obj = Factory::createDb('add');
    $obj->oper(2,4);
  • 相关阅读:
    第二章例2-9
    第二章例2-8
    第二章例2-7
    第二章例2-6
    第二章例2-5
    第二章例2-4
    第二章例2-3
    第二章例2-2
    第二章例2-1
    第一章例1-2
  • 原文地址:https://www.cnblogs.com/isuansuan/p/9760001.html
Copyright © 2011-2022 走看看