Brief analysis of _php example of PHP Factory mode

Source: Internet
Author: User
Tags abstract

This series of articles summarizes the application of design patterns in PHP, which is the second creation model of the factory model.

General introduction to design patterns in the first article, there is no repetition here.

Factory mode

Implementation: Defines an interface for creating objects, letting subclasses decide which class to instantiate.
Scenario: Many subclasses and will expand and create methods more complex.

The factory model is divided into three kinds: simple factory, factory method, abstract factory,

The difference of three plants is that the abstract factory is made up of multiple product lines, and the factory method has only one product line, which is the simplification of the abstract factory. But the factory method and the simple factory opposite, everybody looks like the factory method to add many code at first, but realizes the function and the simple factory. But the essence is that simple factories do not strictly follow the open and closed principles of design patterns, and need to modify the factory code when new products need to be added. But the factory method strictly adheres to the opening and closing principles, the model is responsible for the abstract factory interface, the specific factory to the customer to expand. In the Division of labor, core engineers are responsible for the definition of abstract factories and abstract products, and business engineers are responsible for the implementation of specific factories and products. As long as the abstraction layer is well designed, the framework is very stable.

Copy Code code as follows:

/**
* Factory mode
*/
Abstract Products
Interface Person {
Public function getName ();
}
Specific product realization
Class Teacher implements person {
function GetName () {
Return "Teacher N";
}
}
Class Student implements person {
function GetName () {
Return "Student n";
}
}
Simple Factory
Class Simplefactory {
public static function Getperson ($type) {
$person = null;
if ($type = = ' teacher ') {
$person = new Teacher ();
} elseif ($type = = ' student ') {
$person = new Student ();
}
return $person;
}
}
Simple Factory Call
Class SimpleClient {
function Main () {
If you do not use Factory mode, you need to specify specific classes in advance
$person = new Teacher ();
echo $person->getname ();
$person = new Student ();
echo $person->getname ();
In the Factory mode, you do not need to know what kind of objects to produce, give the factory to decide
$person = Simplefactory::getperson (' teacher ');
echo $person->getname ();
$person = Simplefactory::getperson (' student ');
echo $person->getname ();
}
}
Factory method
Interface Commfactory {
Public function Getperson ();
}
Specific Factory implementation
Class Studentfactory implements Commfactory {
function Getperson () {
return new Student ();
}
}
Class Teacherfactory implements Commfactory {
function Getperson () {
return new Teacher ();
}
}
Factory method call
Class Commclient {
static function main () {
$factory = new Teacherfactory ();
echo $factory->getperson ()->getname ();
$factory = new Studentfactory ();
echo $factory->getperson ()->getname ();
}
}
Abstract Factory mode another product line
Interface Grade {
function getyear ();
}
Another product line of specific products
Class Grade1 implements Grade {
Public Function getyear () {
Return ' Level 2003 ';
}
}
Class Grade2 implements Grade {
Public Function getyear () {
Return ' Level 2004 ';
}
}
Abstract Factory
Interface Abstractfactory {
function Getperson ();
function Getgrade ();
}
Specific factories can produce products for each product line
Class Grade1teacherfactory implements Abstractfactory {
Public Function Getperson () {
return new Teacher ();
}
Public Function Getgrade () {
return new Grade1 ();
}
}
Class Grade1studentfactory implements Abstractfactory {
Public Function Getperson () {
return new Student ();
}
Public Function Getgrade () {
return new Grade1 ();
}
}
Class Grade2teacherfactory implements Abstractfactory {
Public Function Getperson () {
return new Teacher ();
}
Public Function Getgrade () {
return new Grade2 ();
}
}
Abstract Factory Call
Class Factoryclient {
function Printinfo ($factory) {
echo $factory->getgrade ()->getyear (). $factory->getperson ()->getname ();
}
function Main () {
$client = new Factoryclient ();
$factory = new Grade1teacherfactory ();
$client->printinfo ($factory);
$factory = new Grade1studentfactory ();
$client->printinfo ($factory);
$factory = new Grade2teacherfactory ();
$client->printinfo ($factory);
}
}
Simple Factory
Simpleclient::main ();
Factory method
Commclient::main ();
Abstract Factory
Factoryclient::main ();

The small partners understand the PHP design pattern in the Factory mode, is not quite simple, the next article we will introduce the creator mode

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.