PHP Simple Factory mode, factory method mode and abstract Factory mode

Source: Internet
Author: User

PHP Factory mode concept: Factory mode is a class that has some methods for creating objects for you. You can use the factory class to create objects without using new directly. This way, if you want to change the type of object you are creating, you can simply change the factory. All code that uses the factory is automatically changed.
depending on the level of abstraction, the PHP factory model is divided into: simple Factory mode, factory method mode, and abstract Factory mode

Simple Factory mode:

/** * Simple Factory mode comparison with factory method mode.  * Simple Factory is also called Static factory method mode, so the understanding can be determined that the simple factory pattern is created by a static method of the object. */Interfacepeople {functionJiehun ();}classMansImplementspeople{functionJiehun () {Echo' Send the roses and send the ring! <br> '; }} classWomenImplementspeople {functionJiehun () {Echo' Wear a wedding dress! <br> '; }} classSimplefactoty {//static methods in a simple factory    Static functionCreateman () {return NewMan ; }    Static functioncreatewomen () {return Newwomen; }    } $man= Simplefactoty::Createman ();$man-Jiehun ();$man= Simplefactoty::createwomen ();$man->jiehun ();

Factory method Mode:

<?PHP/** Factory Method Mode: * Defines an interface for creating objects, letting subclasses decide which class to instantiate. He can solve the problem of closed open principle in simple Factory mode. <www.phpddt.com Finishing >*/Interfacepeople {functionJiehun ();}classMansImplementspeople{functionJiehun () {Echo' Send the roses and send the ring! <br> '; }} classWomenImplementspeople {functionJiehun () {Echo' Wear a wedding dress! <br> '; }} InterfaceCreateman {//Note that this is a simple factory essential difference, which abstracts the creation of objects into an interface.     functioncreate ();}classFactorymanImplementscreateman{functionCreate () {return  NewMan ; }}classFactorywomenImplementsCreateman {functionCreate () {return Newwomen; }} classClient {//Factory Method    functionTest () {$Factory=NewFactoryman; $man=$Factory-Create (); $man-Jiehun (); $Factory=NewFactorywomen; $man=$Factory-Create (); $man-Jiehun (); }} $f=NewClient;$f->test ();

Abstract Factory mode:

<?PHP/*Abstract Factory: Provides an interface to create a series of related or interdependent objects. Note: The difference between this and the factory method is: A series, and the factory method is one. So, can we think of adding a way to create a "series" of objects in interface create? */Interfacepeople {functionJiehun ();}classOmanImplementspeople{functionJiehun () {Echo' Beauty, I send you roses and rings! <br> '; }}classImanImplementspeople{functionJiehun () {Echo' I secretly like you <br> '; }} classOwomenImplementspeople {functionJiehun () {Echo' I'm going to wear a wedding dress! <br> '; }} classIwomenImplementspeople {functionJiehun () {Echo' I'm so shy!! <br> '; }} InterfaceCreateman {//Note that this is essentially the difference between creating an object and abstracting it into an interface.     functionCreateopen ();//Is divided into introverted and extroverted    functionCreateintro ();//introverted }classFactorymanImplementscreateman{functionCreateopen () {return  NewOman; }    functionCreateintro () {return  NewIman; }}classFactorywomenImplementsCreateman {functionCreateopen () {return  NewOwomen; }    functionCreateintro () {return  NewIwomen; }} classClient {//Abstract Methods    functionTest () {$Factory=NewFactoryman; $man=$Factory-Createopen (); $man-Jiehun (); $man=$Factory-Createintro (); $man-Jiehun (); $Factory=NewFactorywomen; $man=$Factory-Createopen (); $man-Jiehun (); $man=$Factory-Createintro (); $man-Jiehun (); }} $f=NewClient;$f->test ();

Difference:

Simple Factory mode: used to produce any product in the same hierarchy. The inability to add new products to the

Factory mode: Used to produce fixed products in the same grade structure. (Support for adding any product)
Abstract Factory: Used to produce all products of different product families. (for adding new products, powerless; Support for adding product families)

The above three plant methods have different levels of support in two directions: hierarchical structure and product family. So consider which method to use depending on the situation.

Scope of application:

Simple Factory mode:

The factory class is responsible for creating fewer objects, and the customer knows only the parameters of the incoming factory class and does not care about how to create the object.

Factory method Mode:

A factory method pattern can be used when a class does not know that it must create an object's class or a class that expects subclasses to specify the object it creates, when the class delegates the responsibility of creating an object to one of several helper subclasses, and you want to localize the information to which helper subclasses are proxies.

Abstract Factory mode:

A system should not depend on the details of the product class instance being created, composed and expressed, which is important for all forms of the factory model. This system has more than one product family, and the system only consumes one of the product families. Products belonging to the same product family are used together, and this constraint must be reflected in the design of the system. The system provides a library of product classes, with all products appearing on the same interface, so that clients do not rely on implementations.

Whether it is a simple factory model, a factory model, or an abstract factory model, they essentially extract the invariant parts and leave the variable portions as interfaces for maximum reuse. Which design pattern is more appropriate, which depends on the specific business requirements

PHP Simple Factory mode, factory method mode and abstract Factory 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.