php 工厂模式

php 工厂模式

 
主要讲的是简单工程模式。
 

abstract class factory{
    abstract function deal();
}
class coupon extends factory{
    function deal(){
        echo "this is coupon";
    }
}
class point extends factory{
    function deal(){
        echo  "this is point";
    }
}
class log extends factory{
    function deal(){
        echo  "this is log";
    }
}
class action{
    protected static $item;
    function execute($i){
        switch ($i){
            case 'coupon':self::$item=new coupon();break;
            case 'point':self::$item=new point();break;
            case 'log':self::$item=new log();break;
            default:break;        
        }
        if(!empty(self::$item)) self::$item->deal();
    }
}

 

//客户端代码
$custom=new action();
$custom->execute('coupon');

你可能感兴趣的:(工厂模式)