The factory mode includes the common factory mode and the Abstract Factory mode. However, no matter what factory mode, they all have a role, that is, to generate the use of the factory mode of the PHP design mode.
The code is as follows:
/*
* Daily practice of using the PHP design mode factory mode
* The PHP factory model is not hard to understand. as the name suggests, it is a processing factory, and then the factory is a manufacturing product, as long as the manufacturing product
* There must be several elements: "method", "model", and "factory workshop ".
*/
/* Example 1 Common Factory mode
**/
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 ();
}
}
$ Gongchang = new gongchang (); // instantiate the factory
$ Zhangsan = $ gongchang-> getZhangsan (); // manufacturing product
Echo $ zhangsan-> getNames (); // product output function
?>
I wrote an article about the factory design model. In fact, the factory model includes the common factory model and the Abstract factory model. However, no matter what factory model, they all have a role, that is, the generated object.
Well, let's use the simplest example below to demonstrate the factory model in the PHP design mode.
I have summarized three elements of the factory model:
I. product model
II. product instances
3. factory workshop
The code is as follows:
Abstract class prModel {// product model
Abstract function link ();
}
Class webLink extends prModel {// instance 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 a factory
$ Weblink-> link (); // outputs www.jb51.net
?>
The above method briefly describes how to use the factory class. Focus on Object-oriented