zoukankan      html  css  js  c++  java
  • 抽象工厂模式

    <?php
    //青铜会员的打折商品
    class BronzeRebateCommodity
    {
        //描述
        public $descri = '青铜会员的打折商品';
    }
    
    //白银会员的打折商品
    class SilverRebateCommodity
    {
        //描述
        public $descri = '白银会员的打折商品';
    }
    
    //青铜会员的推荐商品
    class BronzeCommendatoryCommodity
    {
        //描述
        public $descri = '青铜会员的推荐商品';
    }
    
    //白银会员的推荐商品
    class SilverCommendatoryCommodity
    {
        //描述
        public $descri = '白银会员的推荐商品';
    }
    
    //各个工厂的接口
    interface ConcreteFactory
    {
        //生产对象的方法
        public function create($what);
    }
    
    //青铜工厂
    class BronzeFactory implements ConcreteFactory
    {
        //生产对象的方法
        public function create($what)
        {
            $productName = 'bronze'.$what.'Commodity';
            return new $productName;
        }
    }
    
    //白银工厂
    class SilverFactory implements ConcreteFactory
    {
        //生产对象的方法
        public function create($what)
        {
            $productName = 'silver'.$what.'Commodity';
            return new $productName;
        }
    }
    
    //调度中心
    class CenterFactory
    {
        //获取工厂的方法
        public function getFactory($what)
        {
            $factoryName = $what.'Factory';
            return new $factoryName;
        }
    }
    
    //建造一个调度中心
    $center = new CenterFactory();
    //获得一个白银会员的工厂
    $factory = $center->getFactory('silver');
    //让工厂制造一个推荐产品
    $product = $factory->create('Commendatory');
    //可以看到是白银会员的推荐商品
    echo $product->descri;
    ?>
  • 相关阅读:
    repair grub in Ubuntu
    DNS attack experiment
    新闻随感(摩托罗拉125亿被Google收购)
    成为C++高手必须要看的书
    nginx
    Nginx Pitfalls
    gcc/gdb
    python 删除文件
    Solve nginx Error 413 Request Entity Too Large
    Solve Nginx Error 413: Request Entity Too Large
  • 原文地址:https://www.cnblogs.com/jiufen/p/4990497.html
Copyright © 2011-2022 走看看