How to use PHP design mode Factory mode
Copy the Code code as follows:
/*
* Daily Practice PHP design mode Factory mode use method
* PHP Factory mode is not difficult to understand, as the name implies, is a processing plant, and then the factory is manufacturing products, as long as the production of products
* There must be several elements: "Method", "model", "Factory workshop".
*/
/* First example General Factory mode
* */
Abstract class Model {//Product models
Abstract function getnames ();
}
Class Zhangsan extends Model {//Product instance
function GetNames () {
Return "My name is Zhengsan";
}
}
Class Lisi extends model{//product example
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 ();//Instance Chemical factory
$zhangsan = $gongchang->getzhangsan ();//Manufacturing Products
echo $zhangsan->getnames ();//Product output function
?>
Before I wrote about the factory design pattern, in fact, the factory model contains common factory patterns and abstract factory patterns, but whatever the factory pattern, they all have a role to play, which is to generate objects.
Well, let's use the simplest example below and then demonstrate the Factory mode in PHP design mode.
I have summed up the three elements of the factory model:
First, the product model
Second, product examples
Third, the factory workshop
Copy the Code code as follows:
Abstract class Prmodel {//product model
Abstract function link ();
}
Class Weblink extends prmodel{//instance one product
Public Function link () {
echo "Www.jb51.net";
}
}
Class Gongchang {//Factory
static public Function Createlink () {
return new Weblink ();
}
}
$weblink =gongchang::createlink ();//Manufacturing an object through a factory
$weblink->link ();//Output www.jb51.net
?>
The above method, it is simple to explain the use of factory class methods. Focus on object-oriented
The above describes the use of the Java Factory mode PHP Advanced Object Building Factory mode, including the Java Factory mode content, I hope to be interested in PHP tutorial friends helpful.