zoukankan      html  css  js  c++  java
  • php设计模式-工厂模式(一)

    <?php
    abstract class Creator{
        /*
            startFactory 返回一个具体的产品
            factoryMethod 返回对象
        */
        protected abstract function factoryMethod(product $product);
        public function startFactory($product)
        {
            return $this->factoryMethod($product);
        }
    }
    //所用的产品统一调用
    class countryFactory extends Creator{
        private $product;
        protected function factoryMethod(product $product)
        {
            $this->product = $product;
            return $this->product->getProperties();
        }
    }
    
    class mysqlFactory extends Creator{
        private $product;
        protected function factoryMethod(product $product)
        {
            $this->product = $product;
            return $this->product->getProperties();
        }
    }
    
    class sqliteFactory extends Creator{
        private $product;
        protected function factoryMethod(product $product)
        {
            $this->product = $product;
            return $this->product->getProperties();
        }
    }
    //共同访问接口
    interface product
    {
        public function getProperties();
    }
    //具体的操作
    class mysqlProduct implements product{
        private $conn;
        public function getProperties()
        {
            return $this->conn="mysql连接成功".PHP_EOL;;
        }
    }
    
    class sqliteProduct implements product{
        private $conn;
        public function getProperties()
        {
            return $this->conn="sqlite连接成功".PHP_EOL;
        }
    }
    
    /**
     * 
     */
    class client
    {
        public $conn;
        public function __construct()
        {
            $this->conn = new countryFactory();
            echo $this->conn->startFactory(new mysqlProduct());
            echo $this->conn->startFactory(new sqliteProduct());
        }
    }
    
    $worker = new client();
  • 相关阅读:
    【shell】日志切割
    【PHP】SVN版本差异导出
    Linux服务器上安装JDK
    Linux 下node环境安装
    CentOS 7.3安装mariadb
    Centos 7.4 自动初始化docker环境,配置docker镜像仓库
    Shell 模板 (Yes or NO)
    Linux修改时间和时区
    lvm方式挂载盘及扩容
    安装ansible
  • 原文地址:https://www.cnblogs.com/zz-952/p/9758156.html
Copyright © 2011-2022 走看看