PHP implementation of design patterns in the abstract factory model, detailed design Patterns _php Tutorial

Source: Internet
Author: User
Tags php example

PHP implementation of design patterns in the abstract factory model, detailed design patterns


Abstract Factory mode (Abstact Factory) is a common software design pattern. This mode 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 series of product families.

Intention

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

"Abstract Factory mode structure diagram"

"Primary role in abstract Factory mode"

Abstract Factory role: it declares an interface that creates an abstract product object. Usually implemented as interfaces or abstract classes, all specific factory classes must implement this interface or inherit this class.

Specific factory (concrete Factory) role: Implements actions to create product objects. The client calls this role directly to create an instance of the product. This role contains the logic to select the appropriate product object. Typically implemented using a specific class.

Abstract product role: Declares an interface for a class of products. It is the parent class of objects created by the factory method pattern, or the interfaces that they collectively own.

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

"The advantages and disadvantages of the abstract factory model"

Advantages of the Abstract factory model:
1, separate the specific class
2. Make it easy to add or replace product families
3, in favor of product consistency

Disadvantages 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. Support for new types of products requires an extended visit to the factory interface, resulting in changes to the Abstractfactory class and all its subclasses.
Abstract Factory is an inclined way to support the addition of new products, it for the new product family to provide convenience, but not for the new product grade structure to provide such convenience.

"Abstract Factory mode for scenarios"

Abstract Factory mode should be used in the following situations:
1. A system should not rely on the details of how a product class instance is created, combined, and expressed, which is important for all forms of the factory model.
2, the system's products have more than one product family, and the system only consumes one of the products of a family.
3, belong to the same product family products are used together, this constraint must be reflected in the design of the system.
4, the system provides a library of product classes, all products with the same interface appear, thereby using the client is not dependent on the implementation
"Java and Mode 189 page"

Several points of the Abstract factory model:

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

Increase in the abstract factory

1. When the number of product hierarchies is constant, adding new product families means adding one (or more) new specific (or abstract and specific) product roles to each product hierarchy. As the factory hierarchy is a registration mechanism parallel to the product hierarchy, the plant grade structure should be adjusted accordingly when the product grade structure is adjusted. Now that new elements appear in the product hierarchy, it is necessary to add the appropriate new elements to the plant hierarchy. In other words, designers simply need to add a new factory class to the system, and there is no need to modify existing plant roles or product roles. Therefore, when the product family in the system increases, the abstract Factory mode supports the "open-close" principle.

2. In the case of a constant number of product families, a new product hierarchy is added. In other words, the number of products in all product hierarchies does not change, but now there is a new product hierarchy that is parallel to the existing product hierarchy structure. To do this, you need to modify all of the factory roles and add a new factory method to each factory class, which is clearly against the "open – closed" principle. In other words, the abstract factory model does not support the "on-closed" 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 when adding its abstract products, does not support the "open-closed" principle. Abstract Factory mode supports the addition of new products in a slanted manner, which facilitates the addition of new product families, and does not provide such convenience for the addition of a higher level structure.

"Abstract Factory mode and other modes"

Singleton mode (singleton mode): The specific factory class can be designed as a singleton class, because the factory usually has a can, so the specific factory sub-class is generally implemented as a singleton.

Factory Method Mode (factory method mode): An abstract factory creates a product that is defined as a factory method.

Prototype mode (prototype mode): If there are multiple possible product families, the specific factory can also use the prototype model, the specific plant used in the product line.

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

"Abstract Factory mode PHP Example"

Copy CodeThe code is as follows:
<?php
/**
* Abstract Factory mode 2010-05-28 SZ
* @author phppan.p#gmail.com
* @package Design pattern
*/

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

/**
* Factory method for creating 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 (), '
';
echo $productB->getname (), '
';
}

}

Client::main ();
?>


The difference between the factory method pattern and the abstract factory model

Factory method Mode:
An abstract product class that can derive a number of specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can only create an instance of a specific product class.

Abstract Factory mode:
Multiple abstract product classes, each abstract product class can derive multiple specific product classes.
An abstract factory class that can derive a number of specific factory classes.
Each specific factory class can create instances of more than one specific product class.

Difference:
The factory method pattern has only one abstract product class, and the abstract factory pattern has multiple.
Factory-mode-specific factory classes can only create instances of a specific product class, while abstract Factory mode may create multiple.

Factory method mode and abstract Factory mode

Abstract factory: a deeper layer than the Factory mode, this time even the factory implementation class do not know, different people can get different factory class. So the abstract factory class is actually a factory class that can produce different factory classes.
The abstract factory method is a factory method that has a two-level build relationship if the "factory-generated object" layer is used as a first-level build relationship. If the actual environment is more complex, can be three levels or even four levels, so you do not think too complex, it is so simple.
Design mode This book is good, but please enlighten him, do not delve into it, because:
design pattern written earlier that time the programmer's understanding of the program is also maintained in the code can be compiled through the good, so you will find that there are many features in the present look like an idiot, it does not seem to say, we usually use this , this is because you are standing in the industrial age to look at the Stone Age products, so do not have to use certain patterns
design mode is not for the Java language, so some features you simply do not use
The actual work encountered in the situation is far more than the design pattern mentioned in the case of simple, some patterns are water you do not use, You don't have to delve into

the best way to do this is to read it quickly, not to read it, or to read it patiently, but not to go into a dead-and-shut, normal reading. After reading you will not be very good, but you again in the next few years of development work back to see a lot of not understand the nature of the pass, in the future development process occasionally will you write code process some inspiration.

 

http://www.bkjia.com/PHPjc/892262.html www.bkjia.com true http://www.bkjia.com/PHPjc/892262.html techarticle The abstract factory pattern in PHP implementation design pattern is detailed, the design pattern is detailed abstract Factory mode (Abstact Factory) is a common software design pattern. This mode is provided for a product family ...

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