Home > Backend Development > PHP Problem > What are the benefits of php factory method pattern

What are the benefits of php factory method pattern

WBOY
Release: 2023-03-15 16:52:01
Original
1503 people have browsed it

The advantage of the PHP factory method pattern is that the core class only needs to pay attention to the interface of the factory class, and the specific product instances are left to the specific factory subclass to create. In this mode, by defining an abstract core factory class and defining the interface for creating products, the work of creating specific product instances is delayed to its factory subclasses.

What are the benefits of php factory method pattern

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What are the benefits of the php factory method pattern

In this pattern, by defining an abstract core factory class and defining an interface for creating product objects, the work of creating specific product instances is delayed to other Factory subclass to complete.

The advantage of this is that the core class only focuses on the interface definition of the factory class, and the specific product instances are left to the specific factory subclass to create. When the system needs to add a new product, there is no need to modify the existing system code. It only needs to add a specific product class and its corresponding factory subclass. This makes the system more scalable and conforms to the opening and closing principle of object-oriented programming.

<?php
interface Animal{
  public function run();
  public function say();
}
class Cat implements Animal
{
  public function run(){
      echo "I ran slowly <br>";
  }
  public function say(){
      echo "I am Cat class <br>";
  }
}
class Dog implements Animal
{
  public function run(){
      echo "I&#39;m running fast <br>";
  }
  public function say(){
      echo "I am Dog class <br>";
  }
}
abstract class Factory{
  abstract static function createAnimal();
}
class CatFactory extends Factory
{
  public static function createAnimal()
  {
      return new Cat();
  }
}
class DogFactory extends Factory
{
  public static function createAnimal()
  {
      return new Dog();
  }
}
 
$cat = CatFactory::createAnimal();
$cat->say();
$cat->run();
 
$dog = DogFactory::createAnimal();
$dog->say();
$dog->run();
Copy after login

The factory method pattern is a further abstraction and promotion of the simple factory pattern. Due to the use of object-oriented polymorphism, the factory method pattern maintains the advantages of the simple factory pattern and overcomes its disadvantages. In the factory method pattern, the core factory class is no longer responsible for the creation of all products, but leaves the specific creation work to subclasses.

This core class is only responsible for giving the interface that a specific factory must implement, and is not responsible for the details of how the product class is instantiated. This allows the factory method pattern to allow the system to introduce new features without modifying the factory role. product.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What are the benefits of php factory method pattern. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template