Home  >  Article  >  Backend Development  >  Design patterns in php: factory pattern

Design patterns in php: factory pattern

WBOY
WBOYOriginal
2016-08-08 09:33:14825browse

A factory class is a class specially used to create other objects. The factory class is very important in the practice of polymorphic programming. It allows dynamic replacement of classes and modification of configurations, making the application more flexible. Mastering the factory pattern is essential for web development.

The factory pattern is usually used to return different classes similar to interfaces. A common use of factories is to create polymorphic providers.

Usually the factory pattern has a key construct, which is a static method usually named factory. This static method can accept any number of parameters and must return an object.

The factory pattern is very important in polymorphic design. If applied properly, it can make the application more portable and the dependencies between classes looser, thus 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:

<spanmicrosoft yahei><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></spanmicrosoft>

Use factory class:

<spanmicrosoft yahei><span>$db</span> = DBFactory::create('mysql'<span>); </span><span>$db</span>->query('show database');</spanmicrosoft>

Every database here inherits the specified interface. The purpose of this is to ensure that 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.

The above introduces the design pattern in PHP: factory pattern, including the content of PHP factory pattern. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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