zoukankan      html  css  js  c++  java
  • PHP设计模式-工厂模式

    代码摘抄自:http://blog.csdn.net/mysteryhaohao/article/details/51181089

    使用场景:

    如果很多地方调用了类Test,那么假设某天需要把Test类的名称修改,则意味着所有调用此类的地方都要修改

    关键:

    只要是可以根据不同的参数生成不同的类实例,那么就符合工厂模式的设计思想

    代码实现:

    <?php  
    /** 
     * 基本工厂模式 
     * */  
    class User {   
        private $username;   
        public function __construct($username) {   
            $this->username = $username;   
        }   
          
        public function getUser() {   
            return $this->username;   
        }   
    }  
      
    class userFactory {   
        static public function createUser() {   
            return new User('Jack');   
        }   
    }  
      
    $user = userFactory::createUser();echo $user->getUser();  
      
    /** 
     *简单工厂模式 
     * */  
    interface userProperties {  
        function getUsername();  
        function getGender();  
        function getJob();  
    }  
    class User implements userProperties{  
        private $username;  
        private $gender;  
        private $job;  
        public function __construct($username, $gender, $job) {  
            $this->username = $username;  
            $this->gender = $gender;  
            $this->job = $job;  
        }  
      
        public function getUsername() {  
            return $this->username;  
        }  
      
        public function getGender() {  
            return $this->gender;  
        }  
      
        public function getJob() {  
            return $this->job;  
        }  
    }  
      
    class userFactory {  
        static public function createUser($properties = []) {  
            return new User($properties['username'], $properties['gender'], $properties['job']);  
        }  
    }  
      
    $employers = [  
        ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'],  
        ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'],  
        ];  
    $user = userFactory::createUser($employers[0]);  
    echo $user->getUsername();  
      
    /** 
     * 工厂方法模式 
     **/  
    interface userProperties {  
        function getUsername();  
        function getGender();  
        function getJob();  
    }  
      
    interface createUser {  
        function create($properties);  
    }  
      
    class User implements userProperties{  
        private $username;  
        private $gender;  
        private $job;  
        public function __construct($username, $gender, $job) {  
            $this->username = $username;  
            $this->gender = $gender;  
            $this->job = $job;  
        }  
      
        public function getUsername() {  
            return $this->username;  
        }  
      
        public function getGender() {  
            return $this->gender;  
        }  
      
        public function getJob() {  
            return $this->job;  
        }  
    }  
      
    class userFactory {  
        private $user;  
        public function __construct($properties = []) {  
            $this->user =  new User($properties['username'], $properties['gender'], $properties['job']);  
        }  
      
        public function getUser() {  
            return $this->user;  
        }  
    }  
      
    class FactoryMan implements createUser {  
        function create($properties) {  
            return new userFactory($properties);   
        }  
    }   
      
    class FactoryWoman implements createUser {  
        function create($properties) {  
            return new userFactory($properties);  
        }  
    }  
      
    class clientUser {  
        static public function getClient($properties) {  
            $fac = new FactoryMan;  
            $man = $fac->create($properties);  
            echo $man->getUser()->getUsername();  
        }  
    }  
      
    $employers = [  
        ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'],  
        ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'],  
        ];  
    $user = clientUser::getClient($employers[0]);  
      
    /** 
     * 抽象工厂模式 
     * */  
      
    interface userProperties {  
        function getUsername();  
        function getGender();  
        function getJob();  
    }  
      
    interface createUser { //将对象的创建抽象成一个接口  
        function createOpen($properties);//内向创建  
        function createIntro($properties);//外向创建  
    }  
      
    class User implements userProperties{  
        private $username;  
        private $gender;  
        private $job;  
        public function __construct($username, $gender, $job) {  
            $this->username = $username;  
            $this->gender = $gender;  
            $this->job = $job;  
        }  
      
        public function getUsername() {  
            return $this->username;  
        }  
      
        public function getGender() {  
            return $this->gender;  
        }  
      
        public function getJob() {  
            return $this->job;  
        }  
    }  
      
    class userFactory {  
        private $user;  
        public function __construct($properties = []) {  
            $this->user =  new User($properties['username'], $properties['gender'], $properties['job']);  
        }  
      
        public function getUser() {  
            return $this->user;  
        }  
    }  
      
    class FactoryMan implements createUser {  
        function createOpen($properties) {  
            return new userFactory($properties);   
        }  
      
        function createIntro($properties) {  
            return new userFactory($properties);   
        }  
    }   
      
    class FactoryWoman implements createUser {  
        function createOpen($properties) {  
            return new userFactory($properties);  
        }  
      
        function createIntro($properties) {  
            return new userFactory($properties);  
        }  
    }  
      
    class clientUser {  
        static public function getClient($properties) {  
            $fac = new FactoryMan;  
            $man = $fac->createOpen($properties);  
            echo $man->getUser()->getUsername();  
        }  
    }  
      
    $employers = [  
        ['username' => 'Jack', 'gender' => 'male', 'job' => 'coder'],  
        ['username' => 'Marry', 'gender' => 'female', 'job' => 'designer'],  
        ];  
    $user = clientUser::getClient($employers[0]); 
  • 相关阅读:
    springboot 踩坑之表单验证
    爬虫学习研究
    selenium+chrome知识
    每天学一点linux命令
    Javascript禁止子元素继承父元素的事件
    sass编译css(转自阮一峰)
    php配置虚拟主机的配置步骤(hosts、httpd.conf、vhosts.conf)1.配置本地的dns文件2.配置apache的主配置文件3.配置Apache的虚拟主机
    修改原代码定制bootstrap
    网页在线进行标准验证
    浏览器兼容性判定写法格式(ie)
  • 原文地址:https://www.cnblogs.com/dongtong/p/7326941.html
Copyright © 2011-2022 走看看