PHP's factory model and abstract factory

Source: Internet
Author: User
Recently looking at the factory model and the abstract factory model, the depth of the PHP book to see this piece two times, but still vaguely, so to ask the degree Niang, I think there is a it so that is very reasonable:

Factory method
The factory approach is to provide a factory class for each product. Create different product instances from different factory instances.
Support for adding any product in the same hierarchy.

Abstract Factory
Abstract factories deal with the concept of product family. For example, every car company may have to produce cars, trucks, and buses at the same time, so every factory has a way to create cars, vans and buses.
It is easy to add new product lines to the product family concept, but it is not possible to add new products.

See Blog for details

In short, the factory method is the production of different products of the same grade, and the abstract factory is the production of different product families, and often the abstract factory will use the factory method.

Let's introduce the factory method first.
Suppose I have a personnel management project, it needs to manage appointment (appointment) objects, be asked to use bloggscal or megacal format to communicate with them, of course, with the development of time, the required format will certainly become more. (In my opinion, the format is equivalent to the product in the Factory mode.)
At the interface level we can define two classes. One is the need to convert the appointment object into a proprietary format, name it the Appencoder class (product products), and the other requires the administrator to get the encoder, which we name the Commsmanager class (creator creator).
Next we have the code:

Abstract classapptencoder{Abstract functionencode();} classbloggsapptencoderextendsapptencoder{ functionencode(){return"Appointment data encode in bloggscal format \ n"; }}Abstract classcommsmanager{Abstract functiongetheadtext();Abstract functiongetapptencoder();Abstract functiongetfootertext();} classbloggscommsmanagerextendscommsmanager{ functiongetheadtext(){return"Bloggscal header\n"; } functiongetapptencoder(){returnNewBloggsapptencoder (); } functiongetfootertext(){return"bloggscal footer \ n"; }}

In this I only wrote a bloggscal format, equivalent to do a product, if you add other such as megacal format as long as the inheritance Apptencoder class and Commsmanager class on the line, this is the factory method.
However, it has not been found that this pattern forms a special code duplication, and may lead to unnecessary instantiation, so when using the Factory mode should be considered clearly

Here's an introduction to the abstract factory,
It says that factory models are used to produce different products and abstract factories are used to generate different product families. Let's take a look at the last example.
At this time the project no longer satisfies only one appointment (appointment) function, he needs to add TTD (to do) and contact (contacts) function, this is to add product family, I write the class diagram, the province of writing code, but also more clear.

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the PHP factory model and abstract factory, including aspects of the content, I hope the PHP tutorial interested in a friend to help.

  • 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.