Comparison of PHP simple factory mode, Factory method mode, and abstract Factory mode

Source: Internet
Author: User
PHP factory mode concept: Factory mode is a type that has some methods to create objects for you. You can use the factory class to create objects instead of using new directly. In this way, if you want to change the type of the created object, you only need to change the factory. All codes used in the factory are automatically changed. Depending on the degree of abstraction, PHP factory models can be divided into simple factory models, Factory method models, and abstract factory models. PHP factory modelConcept: The Factory mode is a type that has some methods for creating objects for you. You can use the factory class to create objects instead of using new directly. In this way, if you want to change the type of the created object, you only need to change the factory. All codes used in the factory are automatically changed.
Depending on the degree of abstraction, PHP factory models can be divided into simple factory models, Factory method models, and abstract factory models.

Simple factory mode:

/*** Compare the simple factory mode with the factory method mode. * A simple factory is also called the static factory method mode. it is clear that the simple factory mode creates an object through a static method. */Interface people {function jiehun ();} class man implements people {function jiehun () {echo 'send rose and ring!
';}} Class women implements people {function jiehun () {echo' wear a wedding dress!
';}} Class SimpleFactoty {// static function createMan () {return new man;} static function createWomen () {return new women ;}} in a simple factory ;}} $ man = SimpleFactoty: createMan (); $ man-> jiehun (); $ man = SimpleFactoty: createWomen (); $ man-> jiehun ();

Factory method mode:

 */Interface people {function jiehun ();} class man implements people {function jiehun () {echo 'send rose and ring!
';}} Class women implements people {function jiehun () {echo' wear a wedding dress!
';}} Interface createMan {// Note: Here is the essential difference between a simple factory and abstracts object creation into an interface. Function create ();} class FactoryMan implements createMan {function create () {return new man;} class FactoryWomen implements createMan {function create () {return new women ;}} class Client {// static function test () {$ Factory = new FactoryMan; $ man = $ Factory-> create (); $ man-> jiehun (); $ Factory = new FactoryWomen; $ man = $ Factory-> create (); $ man-> jiehun ();}} $ f = new Client; $ f-> test ();

Abstract Factory mode:

 ';}} Class Iman implements people {function jiehun () {echo' I secretly like you
';}} Class Owomen implements people {function jiehun () {echo' I want to wear a wedding dress!
';}} Class Iwomen implements people {function jiehun () {echo' I'm so shy !!
';}} Interface createMan {// note that the essential difference is that the object creation is abstracted into an interface. Function createOpen (); // divided into restrained and outgoing function createIntro (); // introverted} class FactoryMan implements createMan {function createOpen () {return new Oman ;} function createIntro () {return new Iman;} class FactoryWomen implements createMan {function createOpen () {return new Owomen;} function createIntro () {return new Iwomen ;}} class Client {// static function test () {$ Factory = new FactoryMan; $ man = $ Factory-> createOpen (); $ man-> jiehun (); $ man = $ Factory-> createIntro (); $ man-> jiehun (); $ Factory = new FactoryWomen; $ man = $ Factory-> createOpen (); $ man-> jiehun (); $ man = $ Factory-> createIntro (); $ man-> jiehun ();}} $ f = new Client; $ f-> test ();

Differences:

Simple factory model: used to produce any product in the same level structure. There is nothing to do with and add new products

Factory model: used to produce fixed products in the same level structure. (Any product can be added)
Abstract factory: used to produce all products of different product families. (There is nothing to do with adding new products; support for adding product families)

The above three factory methods have different levels of support in the hierarchical structure and product family. Which method should be used based on the actual situation?

Applicability:

Simple factory mode:

The factory class is responsible for creating fewer objects. the customer only knows the parameters passed in to the factory class and does not care about how to create objects.

Factory method mode:

When a class does not know the class of the object it must create or a class wants the subclass to specify the object it creates, when the class delegates the responsibility of creating an object to one of multiple help subclasses and you want to localize the information of which help subclass is the proxy, you can use the factory method mode.

Abstract Factory mode:

A system should not depend on the details of product instances such as creation, combination, and expression, which is important for all forms of factory models. This system has more than one product family, and the system only consumes one of them. Products belonging to the same product family are used together. this constraint must be reflected in the system design. The system provides a product library. all products use the same interface, so that the client does not rely on implementation.

Both simple factory mode, Factory mode, and abstract Factory mode extract unchanged parts and save the variable parts as interfaces to maximize reuse. Which design mode is more suitable depends on the specific business needs.

For more articles on comparison of PHP simple factory mode, Factory method mode, and abstract Factory mode, refer to PHP Chinese network!

Related articles:

Detailed description of code for implementing the abstract Factory mode in Java

Abstract Factory pattern of JAVA design pattern

Php object-oriented development-Abstract factory model

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.