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
?>
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 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.