首页 > 代码库 > 设计模式(二)工厂模式

设计模式(二)工厂模式

工厂模式(Factor Pattern) 负责生成其他对象的类或方法

代码实现:

class DemoController extends APP_Controller_Action {

    public function factoryAction(){
        $name = $this->getParam("name");
        $action = $this->getParam("action");
        $child = factory::createClass($name);
        
    }
}

class factory{
     static function createClass($class_name){
         return new $class_name;
     }
}

class child{
    public function test(){
        echo "create child factor success";
    }
}

class adult{
    public function test(){
        echo "create adult factor success";
    }
}

  

设计模式(二)工厂模式