Case Analysis of Abstract Factory Pattern in PHP Design Pattern

  • 2021-12-04 18:16:04
  • OfStack

This paper illustrates the abstract factory pattern of PHP design pattern. Share it for your reference, as follows:

The previous article talked about simple factory (static factory) and factory pattern under PHP. Now let's talk about abstract factory pattern under 1

Factory Mode We have said that it is very simple to add a product, add the corresponding product class and factory class, do not need to change the original code, in line with the principle of opening and closing

Now, following the last article, we have new requirements. We require apple-flavored drinks and banana-flavored drinks. There are two brands, Pepsi and Coca-Cola. With the factory model, it can't be realized. I can only add single 1 products, but I can't achieve it by adding brands. The abstract factory model is extended. The code is as follows


<?php
/**
 * Created by PhpStorm.
 * User: tiansi
 * Date: 18/1/23
 * Time:  Afternoon 5:10
 */
// Factory interface 
interface FruitFactory{
  // Method for producing beverages 
  function makeAppleFruit();
  function makeBananaFruit();
}
// Beverage interface 
interface Fruit{
  function getFruitName();
}
class BaishiAppleFruit implements Fruit{
  function getFruitName()
  {
    echo ' Pepsi Apple Flavor Beverage ';
  }
}
class BaishiBananaFruit implements Fruit{
  function getFruitName()
  {
    echo ' Pepsi banana flavored drink ';
  }
}
class ColeiAppleFruit implements Fruit{
  function getFruitName()
  {
    echo ' Coca-Cola Apple Flavor Beverage ';
  }
}
class ColeBananaFruit implements Fruit{
  function getFruitName()
  {
    echo ' Coca-Cola banana flavored beverage ';
  }
}
// Pepsi Beverage Factory 
class BaishiFruitFactory implements FruitFactory{
  function makeAppleFruit()
  {
    // Production of Pepsi Apple Drink 
    return new BaishiAppleFruit();
  }
  function makeBananaFruit()
  {
    // Production of Pepsi Banana Beverage 
    return new BaishiBananaFruit();
  }
}
// Coca-Cola beverage factory 
class ColeFruitFactory implements FruitFactory{
  function makeAppleFruit()
  {
    // Production of Coca-Cola Apple Beverage 
    return new ColeiAppleFruit();
  }
  function makeBananaFruit()
  {
    // Production of Coca-Cola banana-flavored beverage 
    return new ColeBananaFruit();
  }
}
$baishi_factory = new BaishiFruitFactory();
$baishi_factory->makeAppleFruit()->getFruitName();
echo "<br/>";
$baishi_factory->makeBananaFruit()->getFruitName();
echo "<br/>";
$cole_factory = new ColeFruitFactory();
$cole_factory->makeAppleFruit()->getFruitName();
echo "<br/>";
$cole_factory->makeBananaFruit()->getFruitName();

Run results:

Pepsi Apple Flavor Beverage
Pepsi banana flavored drink
Coca-Cola Apple Flavor Beverage
Coca-Cola banana flavored beverage

Now we can see that if you want to add another brand, you only need to write another factory class and write the corresponding product class, and you can successfully add a brand without changing the original code

But at the moment, if you want to add another single 1 product, it will be very troublesome. You need to change the factory class, which is the difference between factory and abstract factory

Factory mode is aimed at single product and one product abstract class. Each factory can only create one product. It supports extending a single product, but does not support extending a product family. It is aimed at one product grade structure

Abstract factory is aimed at product family and multiple product abstract classes. Each factory can create multiple products. It supports extending product family but does not support extending single product. It is aimed at multiple product grade structures

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: