首页 >后端开发 >php教程 >php的工厂模式

php的工厂模式

不言
不言原创
2018-04-13 16:36:541353浏览

  1. 本篇文章介绍的内容是php的工厂模式,现在分享给大家,有需要的朋友可以参考一下

  2. <?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(&#39;Jack&#39;);   
        }   
    }  
      
    $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[&#39;username&#39;], $properties[&#39;gender&#39;], $properties[&#39;job&#39;]);  
        }  
    }  
      
    $employers = [  
        [&#39;username&#39; => &#39;Jack&#39;, &#39;gender&#39; => &#39;male&#39;, &#39;job&#39; => &#39;coder&#39;],  
        [&#39;username&#39; => &#39;Marry&#39;, &#39;gender&#39; => &#39;female&#39;, &#39;job&#39; => &#39;designer&#39;],  
        ];  
    $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[&#39;username&#39;], $properties[&#39;gender&#39;], $properties[&#39;job&#39;]);  
        }  
      
        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 = [  
        [&#39;username&#39; => &#39;Jack&#39;, &#39;gender&#39; => &#39;male&#39;, &#39;job&#39; => &#39;coder&#39;],  
        [&#39;username&#39; => &#39;Marry&#39;, &#39;gender&#39; => &#39;female&#39;, &#39;job&#39; => &#39;designer&#39;],  
        ];  
    $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[&#39;username&#39;], $properties[&#39;gender&#39;], $properties[&#39;job&#39;]);  
        }  
      
        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 = [  
        [&#39;username&#39; => &#39;Jack&#39;, &#39;gender&#39; => &#39;male&#39;, &#39;job&#39; => &#39;coder&#39;],  
        [&#39;username&#39; => &#39;Marry&#39;, &#39;gender&#39; => &#39;female&#39;, &#39;job&#39; => &#39;designer&#39;],  
        ];  
    $user = clientUser::getClient($employers[0]);

相关推荐:

php模式设计之 单例模式

php模式设计之 适配器模式

以上是php的工厂模式的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn