Analysis on php factory model

Source: Internet
Author: User
This series of articles summarizes the application of the design pattern in PHP. this is the second article on the creation pattern factory pattern. The design pattern is generally introduced in the first article, which won't be repeated here. This series of articles summarizes the application of the design pattern in PHP. this is the second article on the creation pattern factory pattern.

The design pattern is generally introduced in the first article, which won't be repeated here.

Factory model

Implementation: defines an interface for creating objects, so that the subclass determines which class to instantiate.
Application scenarios: a large number of sub-classes are complicated to expand and create.

There are three factory models: simple factory, Factory method, and abstract factory,

The difference between the three factories is that abstract factories are composed of multiple product lines, and the factory method has only one product line, which is simplified by abstract factories. Compared with a simple factory, the factory method seems to have added a lot of code, but the implementation function is the same as that of a simple factory. However, the essence is that simple factories do not strictly follow the open and closed principles of the design model. to add new products, you also need to modify the factory code. However, the factory method strictly complies with the open and closed principles. the mode is only responsible for abstracting factory interfaces, and the specific factory is handed over to the customer for expansion. At work hours, 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 specific products. As long as the abstraction layer is well designed, the framework is very stable.

/*** Factory mode * // abstract product interface Person {public function getName ();} // implement class Teacher implements Person {function getName () {return "instructor 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 calls class SimpleClient {function main () {// if the factory mode is not used, you need to specify a specific class in advance // $ person = new Teacher (); // echo $ person-> getName (); // $ person = new Student (); // echo $ person-> getName (); // In Factory mode, you do not need to know what class the object is generated. you can leave it to the factory to determine $ person = SimpleFactory: getPerson ('teacher'); echo $ person-> getName (); $ person = SimpleFactory: getPerson ('student '); echo $ person-> getName () ;}// factory method interface CommFactory {public function getPerson ();} // the specific factory implements class StudentFactory implements CommFactory {function getPerson () {return new Student () ;}} class TeacherFactory implements CommFactory {function getPerson () {return new Teacher () ;}// the factory method calls class CommClient {static function main () {$ factory = new TeacherFactory (); echo $ factory-> getPerson () -> getName (); $ factory = new StudentFactory (); echo $ factory-> getPerson ()-> getName ();}} // abstract Factory mode interface Grade {function getYear () ;}// the specific product class Grade1 implements Grade {public function getYear () of the other product line () {return '000000';} class Grade2 implements Grade {public function getYear () {return '000000';} // abstract factory interface AbstractFactory {function getPerson (); function getGrade () ;}// a specific factory can generate product 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 () ;}// the abstract factory calls 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 ();

My friends have learned about the factory model in the php design model. is it quite simple.

Related articles:

Sample code in three forms of PHP factory mode

Php factory mode

Benefits of parsing the PHP factory model

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.