>  기사  >  백엔드 개발  >  PHP 공장 모드

PHP 공장 모드

不言
不言원래의
2018-04-13 16:36:541325검색

  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으로 문의하세요.