Use of PHP advanced Object Construction factory Mode

Source: Internet
Author: User

PHP design mode factory mode usage
Copy codeThe Code is as follows:
<? Php
/*
* 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
Copy codeThe Code is as follows:
<? Php
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.