php工厂设计模型

时间:2021-05-24 14:24:42   收藏:0   阅读:0
<?php
class A{
    function index (){
        echo ‘我是A的index‘;
    }
}

class B{
    function index (){
        echo ‘我是B的index‘;
    }
}

class C{
    function index (){
        echo ‘我是C的index‘;
    }
}

class D{
    private static $class_name;
    public static function fatory($name){
        self::$class_name =  new $name;
        return self::$class_name;
    }
}

$e = D::fatory(‘A‘)-》;

var_dump($e);

思考:

  1.有多个需要实例化的类

  2.通过工厂类去管理多个需要使用的类

  3.通过工厂提供的方法去实例化需要的类

  4.降低了代码的耦合性

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!