Java Factory mode PHP Advanced Object build Factory mode usage

Source: Internet
Author: User
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.

  • Related Article

    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.