Home > Backend Development > PHP Tutorial > Java Factory Pattern PHP Advanced Object Construction Usage of Factory Pattern

Java Factory Pattern PHP Advanced Object Construction Usage of Factory Pattern

WBOY
Release: 2016-07-29 08:47:58
Original
886 people have browsed it

How to use PHP design pattern factory pattern

Copy the code The code is as follows:


/*
* Practice how to use PHP design pattern factory pattern every day
* PHP factory pattern is not difficult Understand, as the name suggests, it is a processing factory, and the factory manufactures products. As long as the product is manufactured
*, it must have several elements: "method", "model", and "factory workshop".
*/
/*First example ordinary factory model
* */
abstract class model {//Product model
abstract function getNames();
}
class zhangsan extends model {//Product instance
function getNames() {
return "my name is zhengsan";
}
}
class lisi extends model{//Product instance
function getNames(){
return "my name is lisi";
}
}
abstract class gongchangModel {// Factory model
abstract function getZhangsan();
abstract function getLisi();
}
class gongchang extends gongchangModel{//Factory instance
function getZhangsan(){
return new zhangsan();
}
function getLisi(){
return new lisi();
}
}
$g gongchang();//Instantiate factory
$zhangsan=$gongchang->getZhangsan();//Manufacturing products
echo $zhangsan->getNames(); //Product output function
?>


I wrote an article about the factory design pattern before. In fact, the factory pattern includes the ordinary factory pattern and the abstract factory pattern. However, no matter what the factory pattern is, they all have a The function is to generate objects.
Okay, let’s use the simplest example below to demonstrate the factory pattern in the PHP design pattern.
I summarized it myself, the three elements of the factory model:
1. Product model
2. Product examples
3. Factory workshop

Copy the code The code is as follows:


abstract class prModel {//Product model
abstract function link();
}
class webLink extends prModel{//Example a product
public function link(){
echo "www.jb51.net";
}
}
class gongchang {//Factory
static public function createLink (){
return new webLink();
}
}
$weblink=gongchang::createLink();//Create an object through the factory
$weblink->link() ;//Output www.jb51.net
?>


The above method simply explains how to use the factory class. Pay attention to object-oriented

The above introduces the use of java factory mode, PHP advanced object construction and factory mode, including the content of java factory mode. I hope it will be helpful to friends who are interested in PHP tutorials.

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