Home > Backend Development > PHP Tutorial > PHP design pattern 2: factory mode, singleton mode, registration tree mode

PHP design pattern 2: factory mode, singleton mode, registration tree mode

不言
Release: 2023-03-23 18:36:02
Original
1340 people have browsed it

This article introduces the content of PHP design pattern 2: factory mode, singleton mode, and registration tree mode. Now I share it with you. Friends in need can refer to it.

1. Factory Pattern: Generate objects in factory methods or classes instead of directly new in the code
Advantages: When a common class needs new in multiple places, use the factory pattern to facilitate the expansion and maintenance of the class
File directory: Frame/Factory.php

<?php
namespace Frame;
use Frame\Database;
use Frame\Register;

class Factory
{
    static function createDatabase()
    {
        //$db = new Database(); //正常实例化类
        $db = Database::getInstance();    //获取单例模式的类
        Register::set(&#39;db&#39;,$db);          //将实例化后的类注册到全局注册树中
        return $db;
    }
}
//外部调用得到$db对象
$db = Frame\Factory::createDatabase();
//获取全局注册树中的对象
$db = Frame\Register::get(&#39;db&#39;);
//卸载全局注册树中的对象
$db = Frame\Register::_unset(&#39;db&#39;);
?>
Copy after login

2. Singleton mode: Only new instantiation objects are allowed in the class itself
Advantages: The class cannot be new externally, and The object is created once in the class itself, saving resource overhead
File directory: Frame/Database.php

<?php
namespace Frame;

class Database
{
    protected $db;
    
    //单例模式,私有化__construct()方法,不允许外部实例化对象
    private function __construct()
    {
    }
    //实例化本类
    static function getInstance()
    {
        if(self::$db) {
            return self::$db;
        } else {
            self::$db = new self();
            return self::$db;
        }
    }
}
//外部调用
$db = Frame\Database::getInstance();
?>
Copy after login

3. Registration tree mode: Register an object into the global registration tree for convenience Global use
Storage directory: Frame/Register.php

<?php
namespace Frame;

class Register
{
    protected $objects;
    /*
    *    将实例化后的类注册到全局注册树中
    *    $alias :对象别名
    *    $object:实例化后的对象
    */
    static function set($alias,$object)
    {
        self::$objects[$alias] = $object;
    }
    /*
    *    卸载实例化后的类
    *    $alias :对象别名
    */
    static function _unset($alias)
    {
        unset(self::$objects[$alias]);
    }
    /*
    *    获取实例化后的类对象
    *    $alias : 对象别名
    */
    static function get($alias)
    {
        return self::$objects[$alias];
    }
    
}
?>
Copy after login

Related recommendations:

php design pattern one namespace, automatic loading class, PSR-0 Coding Standards

The above is the detailed content of PHP design pattern 2: factory mode, singleton mode, registration tree mode. 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