The abstract factory pattern in PHP implementation design pattern detailed _php skill

Source: Internet
Author: User

Abstract Factory mode (Abstact Factory) is a common software design pattern. This pattern provides a unified creation interface for a product family. When you need a series of this product family, you can create a specific factory class for this family of product families.

Intention

Abstract Factory mode provides an interface to create a system-dependent or interdependent object without specifying their specific class "GOF95"

"Abstract factory pattern structure diagram"

"Primary role in abstract Factory mode"

Abstract Factory role: it declares an interface to create abstract product objects. Typically implemented as an interface or abstract class, all specific factory classes must implement this interface or inherit this class.

Specific factory (concrete Factory) role: Implements the operation of creating product objects. The client invokes this role directly to create an instance of the product. This role contains the logic to choose the right product object. Typically, you use a specific class implementation.

Abstract product role: an interface that declares a class of products. It is the parent class of objects created by the factory method pattern, or the interfaces they have in common.

Concrete Product Role: Implements an interface defined by an abstract product role, defining a product object that will be created by the corresponding specific factory. It contains the business logic of the application internally.

"The advantages and disadvantages of the abstract factory model"

The advantages of abstract Factory mode:
1, the separation of specific classes
2. Make it easier to add or replace product families
3, conducive to product consistency

The drawback of abstract Factory mode: It is difficult to support new kinds of products. This is because the Abstractfactory interface determines the collection of products that can be created. Supporting new types of products requires extending the visit to the factory interface, leading to changes in the Abstractfactory class and all its subclasses.
Abstract Factory is to support the addition of new products in an oblique way, it provides convenience for the increase of new product family, and cannot provide such convenience for the increase of the class structure.

"The abstract factory pattern applies to the scene"

The abstract factory pattern should be used in the following situations:
1. A system should not rely on the details of how product class instances are created, assembled, and expressed, which is important for all forms of Factory mode.
2, the system's products have more than one product family, and the system consumes only one of the products of a family.
3. Products belonging to the same product family are used together, this constraint must be embodied in the design of the system.
4, the system provides a product class library, all products with the same interface, so that the use of the client does not depend on the implementation
"Java and Mode 189 pages"

Several points of Abstract factory pattern:

1. It is not necessary to use the abstract Factory mode if there is no need to change the "multi-Series object build" requirement.
2, "series of objects" refers to the object is interdependent, or the relationship between the role.
3, Abstract Factory mode is mainly to deal with the "new series" demand changes. Disadvantages are difficult to deal with
Changes in the demand for "new objects". This point should be noted, as mentioned earlier, if we are going to join
Other series of classes, the code changes will be very large.
4. Abstract Factory mode is often combined with factory method to deal with
Change in the requirements for object creation.

An increase in the abstract factory

1. In the case of a constant number of product hierarchies, adding new product families means adding one (or more) new specific (or abstract) and specific product roles in each product hierarchy. Because the factory grade structure is the registration organization which is parallel with the product grade structure, therefore, when the product grade structure has the adjustment, needs the factory grade structure to make the corresponding adjustment. Now that there are new elements in the product hierarchy, it is necessary to add the corresponding new elements to the factory hierarchy structure. In other words, designers just need to add a new specific factory class to the system, there is no need to modify the existing factory role or product role. Therefore, when the product family in the system increases, the abstract factory model supports the "open-closed" principle.

2. Add a new product hierarchy to the same number of product families. In other words, the number of products in all product hierarchies does not change, but there is now a new product hierarchy that is parallel to the existing product hierarchy structure. To do this, you need to modify all the factory roles and add a new factory approach to each plant class, which is clearly contrary to the "open-closed" principle. In other words, the abstract factory model does not support the "open – close" principle for the increase of the product hierarchy.

Together, we can know that in the existing abstract products to add their specific products, support the "open-closed principle", but in addition to its abstract products, do not support the "open-closed" principle. Abstract Factory mode supports the addition of new products in an oblique way, which facilitates the growth of new product families, and does not provide such convenience for the increase in the class structure.

"Abstract Factory mode and other patterns"

Single case mode (singleton mode): The specific factory class can be designed as a single case class, because the factory usually has one, so the specific factory subclass is generally implemented as a singleton.

Factory approach Mode (Factory method mode): Abstract factory Creating a product is defined as a factory method.

Prototype mode (prototype mode): If there are multiple possible product lines, the specific factory can also use the prototype model, the specific factory using the product line.

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

"Abstract Factory mode PHP sample"

Copy Code code as follows:

<?php
/**
* Abstract Factory mode 2010-05-28 SZ
* @author phppan.p#gmail.com
* @package Design
*/

/**
* Abstract Factory
*/
Interface Abstractfactory {
/**
* Factory method to create a product with a hierarchical structure of a
*/
Public function createproducta ();

/**
* Factory method to create a product with a hierarchical structure of B
*/
Public function CREATEPRODUCTB ();

}

/**
* Specific Factory 1
*/
Class ConcreteFactory1 implements abstractfactory{

Public Function createproducta () {
return new ProductA1 ();
}

Public Function Createproductb () {
return new ProductB1 ();
}
}


/**
* Specific Factory 2
*/
Class ConcreteFactory2 implements abstractfactory{

Public Function createproducta () {
return new ProductA2 ();
}

Public Function Createproductb () {
return new ProductB2 ();
}
}

/**
* Abstract Product A
*/
Interface Abstractproducta {

/**
* Get Product Name
*/
Public function getName ();
}

/**
* Abstract Product B
*/
Interface ABSTRACTPRODUCTB {

/**
* Get Product Name
*/
Public function getName ();
}

/**
* Specific Product A1
*/
Class ProductA1 implements Abstractproducta {
Private $_name;

Public Function __construct () {
$this->_name = ' product A1 ';
}

Public Function GetName () {
return $this->_name;
}
}


/**
* Specific Product A2
*/
Class ProductA2 implements Abstractproducta {
Private $_name;

Public Function __construct () {
$this->_name = ' product A2 ';
}

Public Function GetName () {
return $this->_name;
}
}


/**
* Specific Product B1
*/
Class ProductB1 implements ABSTRACTPRODUCTB {
Private $_name;

Public Function __construct () {
$this->_name = ' product B1 ';
}

Public Function GetName () {
return $this->_name;
}
}

/**
* Specific 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 factory instance to generate product, output product name
* @param $factory Abstractfactory Factory Example
*/
public static function run (Abstractfactory $factory) {
$productA = $factory->createproducta ();
$productB = $factory-&GT;CREATEPRODUCTB ();
echo $productA->getname (), ' <br/> ';
echo $productB->getname (), ' <br/> ';
}

}

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.