Home > Backend Development > PHP Tutorial > PHP Design Patterns: Introduction to PHP Factory Pattern (with code)

PHP Design Patterns: Introduction to PHP Factory Pattern (with code)

不言
Release: 2023-04-03 15:54:01
Original
1906 people have browsed it

The content of this article is about the PHP design pattern: an introduction to the PHP factory pattern (with code). It has a certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Factory pattern is a method that hides the instantiation details of a class, and uses the same instantiation method for a series of classes, which can easily expand the way to create objects for more classes that implement the same interface.
Example application scenarios include:
1) Online payment
There are many payment methods, such as WeChat payment, Alipay payment, UnionPay payment, etc. The corresponding payment method is adopted according to the user's choice;
2 ) Visit statistics
In a project with a more complex business, users access different objects, such as accessing articles, accessing authors, accessing article directories, etc. The same access record has different recorded information

Principle of the factory pattern:
First design an interface based on the same behavior of multiple objects, then let these classes implement this interface, and then create an instantiation factory, which can be instantiated according to different parameters in the factory Different types.

Specific implementation of factory mode:

/*
 *  形状接口
 */interface Shape{
    public function area();}/*
 * 长方形类,实现了形状接口
 */class Rectangle implements Shape{
    private $long;    private $width;    function __construct($long,$width)
    {
        $this->long = $long;        $this->width = $width;
    }    /*
     * 实现面积方法
     */
    public function area()
    {
        // TODO: Implement area() method.
        return $this->long * $this->width;
    }
}/*
 * 正方形类,实现了形状接口
 */class Square implements Shape{
    private $width;    function __construct($width)
    {
        $this->width = $width;
    }    /*
     * 实现面积方法
     */
    public function area()
    {
        // TODO: Implement area() method.
        return pow($this->width,2);
    }
}/*
 * 圆形类,实现了形状接口
 */class Circle implements Shape{
    private $radiu;    function __construct($radiu)
    {
        $this->radiu = $radiu;
    }    /*
     * 实现面积方法
     */
    public function area()
    {
        // TODO: Implement area() method.
        return pi()*pow($this->radiu,2);
    }
}class ShapeFactory{
    /*
     * 获取实例化的形状对象
     * ...$args 表示接受不限个数的参数
     */
    public function getShpae(...$args)
    {
        /*
         * 第一个参数为形状名,后面的参数为形状的尺寸
         */
        switch($args[0])
        {            case 'Rectangle':                return new Rectangle($args[1],$args[2]);            break;            case 'Square':                return new Square($args[1]);            break;            case 'Circle':                return new Circle($args[1]);            break;
        }        return null;
    }
}$factory = new ShapeFactory();$shape = $factory->getShpae('Rectangle',2,3);$area[] = $shape->area();$shape = $factory->getShpae('Square',2);$area[] = $shape->area();$shape = $factory->getShpae('Circle',2);$area[] = $shape->area();
print_r($area);/*
Array
(
    [0] => 6
    [1] => 4
    [2] => 12.566370614359
)
*/
Copy after login

Recommended related articles:

How to obtain the first-level directory of files in php (pure code)

phpHow to use longitude and latitude to calculate the distance between two points (pure code)

The above is the detailed content of PHP Design Patterns: Introduction to PHP Factory Pattern (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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