Detailed description of the Abstract Factory mode in PHP implementation design mode

Source: Internet
Author: User
Tags php example
This article mainly introduces the detailed description of the Abstract Factory mode in PHP implementation design mode. The Abstract Factory mode is a common software design mode, for more information, see Abstract Factory. it is a common software design model. This mode provides a unified creation interface for a product family. When you need a series of product families, you can create a specific factory class for these product families.

[Intention]

Abstract Factory mode provides an interface for creating system-related or mutually dependent objects without specifying their specific classes [GOF95]

[Abstract Factory pattern structure diagram]

[Abstract the main role of the factory model]

Abstract Factory role: it declares an interface for creating Abstract product objects. It is usually implemented using an interface or abstract class. all factory classes must implement this interface or inherit this class.

Concrete Factory role: create a product object. The client directly calls this role to create a product instance. This badge contains the logic for selecting the right product object. It is usually implemented using a specific class.

Abstract Product role: declares the interfaces of a Product. It is the parent class of the object created in the factory method mode, or the interfaces they share.

Concrete Product role: implements the interface defined by the abstract Product role and defines a Product object that will be created by a specific factory. It contains the business logic of the application.

[Abstract advantages and disadvantages of the factory model]

Advantages of the Abstract factory model:
1. separated specific classes
2. make it easy to add or replace product families
3. conducive to product consistency

Disadvantages of the Abstract factory model: it is difficult to support new types of products. This is because the AbstractFactory interface determines the product set that can be created. To support various new products, you need to extend the access factory interface, resulting in changes to the AbstractFactory class and all its subclasses.
Abstract factory supports the addition of new products in a skewed manner. it facilitates the addition of new product families, rather than providing convenience for the addition of new product level structures.

[Application scenarios of Abstract factory models]

The abstract factory model should be used in the following situations:
1. a system should not depend on the details of how product instances are created, combined, and expressed. this is important for all forms of factory models.
2. there are more than one product family in this system, and the system only consumes products of one of them.
3. products belonging to the same product family are used together. this constraint must be reflected in the system design.
4. The system provides a product library. all products use the same interface, so that the client does not rely on the implementation.
[Java and mode page 1]

Abstract Factory mode:

1. if the requirement for "multi-series object building" is not changed, the Abstract Factory mode is not necessary.
2. "series object" refers to the relationship between objects that are mutually dependent or act.
3. the Abstract Factory mode mainly aims to cope with the changes in the needs of the new series. The disadvantage is that it is hard to cope
Requirement changes for "new object. This should be noted, as mentioned above, if we want to add
For other series of classes, the code will be greatly changed.
4. the Abstract Factory mode is often combined with the Factory Method mode.
Requirement changes for "object creation.

Add in Abstract factory

1. when the number of product level structures remains the same, adding a new product family means adding one or more products in each product level structure) new (or abstract and specific) product roles. Because the factory level structure is a registration institution parallel to the product level structure, when the product level structure is adjusted, the factory level structure needs to be adjusted accordingly. Now there are new elements in the product level structure. Therefore, you need to add new elements to the factory level structure. In other words, the designer only needs to add a new factory class to the system, and there is no need to modify the existing factory role or product role. Therefore, when the product family in the system is increased, the abstract Factory mode supports the "open-close" principle.

2. add a new product level structure when the number of product families remains unchanged. In other words, the number of products in the product level structure will not change, but now there is a new product level structure parallel to the existing product level structure. To achieve this, we need to modify all the factory roles and add a new factory method to each factory class. this is obviously against the "open-close" principle. In other words, the abstract factory model does not support the "open-close" principle when the product level structure is increased.

In combination, we can know that adding specific products to existing abstract products supports the "open-close principle". However, when adding abstract products, it does not support the "open-close" principle. Abstract Factory mode supports the addition of new products in a skewed manner, which facilitates the addition of new product families, rather than facilitating the addition of new product level structures.

[Abstract Factory mode and other modes]

Singleton mode: a specific factory class can be designed as a Singleton class. because a factory can usually have one, a specific factory subclass is generally implemented as a singleton.

Factory method mode: the method used to abstract a factory to create a product is defined as a factory method.

Prototype: if there are multiple possible product series, the specific factory can also use the prototype. the specific factory uses the product series.

The prototype of each product is instantiated and a new product is created by copying its prototype.

PHP example of abstract Factory mode]

The code is as follows:


<? Php
/**
* Abstract factory model 2010-05-28 sz
* @ Author phppan. p # gmail.com
* @ Package design pattern
*/

/**
* Abstract factory
*/
Interface AbstractFactory {
/**
* How to create A factory for A product with A hierarchical structure of
*/
Public function createProductA ();

/**
* How to create a factory for a product whose level structure is B
*/
Public function createProductB ();

}

/**
* Factory 1
*/
Class ConcreteFactory1 implements AbstractFactory {

Public function createProductA (){
Return new ProductA1 ();
}

Public function createProductB (){
Return new ProductB1 ();
}
}


/**
* Factory 2
*/
Class ConcreteFactory2 implements AbstractFactory {

Public function createProductA (){
Return new ProductA2 ();
}

Public function createProductB (){
Return new ProductB2 ();
}
}

/**
* Abstract product
*/
Interface AbstractProductA {

/**
* Get the product name
*/
Public function getName ();
}

/**
* Abstract product B
*/
Interface AbstractProductB {

/**
* Get the product name
*/
Public function getName ();
}

/**
* Specific product A1
*/
Class ProductA1 implements AbstractProductA {
Private $ _ name;

Public function _ construct (){
$ This-> _ name = 'produca1 ';
}

Public function getName (){
Return $ this-> _ name;
}
}


/**
* Product A2
*/
Class ProductA2 implements AbstractProductA {
Private $ _ name;

Public function _ construct (){
$ This-> _ name = 'product A2 ';
}

Public function getName (){
Return $ this-> _ name;
}
}


/**
* Product B1
*/
Class ProductB1 implements AbstractProductB {
Private $ _ name;

Public function _ construct (){
$ This-> _ name = 'product B1 ';
}

Public function getName (){
Return $ this-> _ name;
}
}

/**
* Product B2
*/
Class ProductB2 implements AbstractProductB {
Private $ _ name;

Public function _ construct (){
$ This-> _ name = 'product B2 ';
}

Public function getName (){
Return $ this-> _ name;
}
}


/**
* Client
*/
Class Client {

/**
* Main program.
*/
Public static function main (){
Self: run (new ConcreteFactory1 ());
Self: run (new ConcreteFactory2 ());
}

/**
* Call a factory instance to generate a product and output the product name
* @ Param $ factory AbstractFactory factory instance
*/
Public static function run (AbstractFactory $ factory ){
$ ProductA = $ factory-> createProductA ();
$ ProductB = $ factory-> createProductB ();
Echo $ productA-> getName (),'
';
Echo $ productB-> getName (),'
';
}

}

Client: main ();
?>

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.