Learn design patterns with me (2), learn design patterns_PHP tutorial

WBOY
Release: 2016-07-13 10:12:55
Original
877 people have browsed it

Learn design patterns with me (2), learn design patterns

The factory pattern is very important in polymorphic design. If applied properly, it can make the application The portability is better, and the dependencies between classes are looser, thereby improving flexibility. If the singleton pattern is considered the responsibility of the class, then the factory pattern is the polymorphism of the class.

So what is a factory class?

Concept: A factory class refers to a class that contains a method specifically used to create other objects.

Application scenario: Factory pattern is usually used to return different classes that conform to similar interfaces. In other words, factory classes allow us to decide which class to instantiate based on configuration or application logic.

The following is a simple factory class code:

<span><span>interface</span><span> IDatabase {
  //...</span><span>public</span> <span>function</span> query(<span>$sql</span><span>);</span><span>
}

</span><span>class</span><span> DBFactory {

    </span><span>public</span> <span>static</span> <span>function</span> create(<span>$type</span><span>){
        </span><span>$db</span> = <span>null</span><span>;
        </span><span>switch</span> (<span>$type</span><span>) {
            </span><span>case</span> 'mysql':
                <span>$db</span> = <span>new</span> <span>Mysql</span>(<span>/*</span><span>*arguments</span><span>*/</span><span>);
                </span><span>break</span><span>;
            </span><span>case</span> 'sqlite':
                <span>$db</span> = <span>new</span> Sqlite(<span>/*</span><span>*arguments</span><span>*/</span><span>);
                </span><span>break</span><span>;
            </span><span>case</span> 'pgsql':
                <span>$db</span> = <span>new</span> PGsql(<span>/*</span><span>*arguments</span><span>*/</span><span>);
                </span><span>break</span><span>;
            </span><span>default</span>:
                <span>#</span><span> code...</span>
                <span>break</span><span>;
        }

        </span><span>return</span> <span>$db</span><span>;
    }

}

</span><span>class</span> <span>Mysql</span> <span>implements</span><span> IDatabase {
   </span><span>//...
    </span><span>public</span> <span>function</span> query(<span>$sql</span><span>){

    }</span><span>
}
</span><span>/*</span><span>*other class ...</span><span>*/</span></span>
Copy after login

Use factory class:

<span><span>$db</span> = DBFactory::create('mysql'<span>);
</span><span>$db</span>->query('show database');</span>
Copy after login

Every database here inherits the specified interface. The purpose of this is to make all database objects have consistent external performance. External classes can safely use the methods declared in the interface, which is what we often call transparent to users in software engineering. If one day, due to changes in the computer room, we want to switch to another database, we only need to implement the relevant database classes according to the interface, and the business code does not need to be changed. This reflects the flexibility and polymorphism of the factory class.

From another perspective, we concentrated all the changes at the entrance. There is no need to perform repeated if-else processing internally for these changes.

Okay, there is only so much theoretical content. More experience needs to be applied in projects and to appreciate its benefits.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/918064.htmlTechArticleLearn design patterns with me (2). Learn design patterns. The factory pattern is very important in polymorphic design. If applied properly, it can make the application more portable, and the dependencies between classes...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template