美文网首页
工厂方法模式(factory method模式)

工厂方法模式(factory method模式)

作者: 苏康申 | 来源:发表于2020-03-07 01:05 被阅读0次
    //-----------抽象产品角色------------
    
    //定义保险产品工厂获取产品
    interface InsuranceProduct
    {
        public function getProductList();
    }
    
    //-----------具体产品角色------------
    //众安保险
    class ZhongAn implements InsuranceProduct {
    
        public function getProductList()
        {
            return "ZhongAn Insurance ProductList";
        }
    } 
    
    //小雨伞保险
    class XiaoYuSan implements InsuranceProduct {
        
        public function getProductList()
        {
            return "XiaoYuSan Insurance ProductList";
        }
    } 
    
    //齐欣保险
    class QiXin implements InsuranceProduct {
    
        public function getProductList()
        {
            return "QiXin Insurance ProductList";
        }
    } 
    
    //-----------抽象工厂类------------
    interface Factory
    {
        public function create();
    }
    
    
    //-----------继承工厂类, 用于实例化产品------------
    class ZhongAnFactory implements Factory 
    {
        public function create()
        {
            return new ZhongAn();
        }
    }
    
    class XiaoYuSanFactory implements Factory 
    {
        public function create()
        {
            return new XiaoYuSan();
        }
    }
    
    class QiXinFactory implements Factory 
    {
        public function create()
        {
            return new QiXin();
        }
    }
    
    //-----------具体操作类------------
    class Client
    {
        public function test()
        {
            $qiXinResult = new QiXinFactory();
            echo $qiXinResult->create()->getProductList();
    
            $xiaoYuSanResult = new XiaoYuSanFactory();
            echo $xiaoYuSanResult->create()->getProductList();
        }
    }
    
    $lala = new Client();
    $lala->test(); 
    

    相关文章

      网友评论

          本文标题:工厂方法模式(factory method模式)

          本文链接:https://www.haomeiwen.com/subject/atjtdhtx.html