zoukankan      html  css  js  c++  java
  • 工厂模式 接口 封装 实例

    <?php
    /*
    使用接口
    制造厂和实例化直接参数用数组封装,告诉工厂对谁,做什么,实例化的代码就不再做其他事情
    */
    //各种兵来一个接口
    interface bingge
    {
        public function make();//制造
        public function weixiu();//维修
    }
    
    //火焰兵
    class huoyan implements bingge
    {
        public function make()
        {
            echo "我是一个火焰兵";
        }
    
        public function weixiu()
        {
            echo "我在维修一个火焰兵";
        }
    }
    
    //机枪兵
    class jiqiang implements bingge
    {
        public function make()
        {
            echo "我是一个机枪兵";
        }
    
        public function weixiu()
        {
            echo "我在维修一个机枪兵";
        }
    }
    
    //如果第二期需要:坦克兵,就只用在这里添加
    class tanke implements bingge
    {
        public function make()
        {
            echo "我是一个坦克兵!";
        }
    
        public function weixiu()
        {
            echo "我在维修一个坦克兵!";
        }
    }
    
    //建造士兵的工具
    class makeshibing
    {
        public function zuosa($canshu)
        {
            $comeon = new $canshu["who"];
            $comeon->$canshu["what"]();
        }
    }
    
    //一个建造实例
    $begin = new makeshibing();
    $canshu = array
    (
        "who"  =>  "huoyan" ,
        "what"  =>  "make" ,
    );
    $how = $begin->zuosa($canshu);
    
    echo "<hr>";
    
    //第二期需要增加坦克兵种
    $begin = new makeshibing();
    $canshu = array
    (
        "who"  =>  "tanke" ,
        "what"  =>  "make" ,
    );
    $how = $begin->zuosa($canshu);
    
    echo "<hr>";
    
    //第三期需要
    $begin = new makeshibing();
    $canshu = array
    (
        "who"  =>  "tanke" ,
        "what"  =>  "weixiu" ,
    );
    $how = $begin->zuosa($canshu);
    ?>
  • 相关阅读:
    web程序入门六(缓存)
    web程序入门五(http无状态)
    web程序入门四(webform常用成员)
    web程序入门三(分页)
    web程序入门二(webforms web窗体 aspx)
    CodeForce 710E
    CodeForce 710C Magic Odd Square
    CodeForce 710B Optimal Point on a Line
    CodeForce 710A King Moves
    HDU 4003
  • 原文地址:https://www.cnblogs.com/jiufen/p/4990629.html
Copyright © 2011-2022 走看看