Blogger Information
Blog 22
fans 1
comment 1
visits 22664
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php常用设计模式(单例,工厂,注册树模式)
forever浅笑
Original
966 people have browsed it

单例模式

实例

<?php

/医院
 * 单例模式
 */
class Single
{
    private static $instance = null;

    // 不允许实例化
    private function __construct() { }

    // 不允许克隆
    private function __clone() { }

    // 获取对象
    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    // 从1加到100
    public function total() {
        $s = 0;
        for ($i = 1; $i <= 100; $i++) {
            $s = $s + $i;
        }
        echo $s;
    }
}

$obj1 = Single::getInstance();
var_dump($obj1);  // object(Single)#1 (0) { }
$obj2 = Single::getInstance();
var_dump($obj2); // object(Single)#1 (0) { }
echo '<hr>';
$obj1->total();

// 单例模式总结
// 1. 创建一个null的静态实例
// 2. 不允许实例化,不允许克隆
// 3. 创建一个静态方法判断并获取返回对象

运行实例 »

点击 "运行实例" 按钮查看在线实例


简单工厂模式 


实例

<?php

/医院
 *  简单工厂模式
 */
class ProductFactory
{
    public static function create($type)
    {
        switch ($type) {
            case 'A':
                return new ProductA();
                break;
            case 'B':
                return new ProductB();
                break;
        }
    }
}

class ProductA {
    public function __construct()
    {
        echo 'this is productA';
    }
}

class ProductB {
    public function __construct()
    {
        echo 'this is productB';
    }
}

ProductFactory::create('A');  // this is productA
echo '<hr>';
ProductFactory::create('B'); // this is productB

// 总结:
    //工厂模式与单例模式只产生单一实例不同,工厂模式用于创建多种类型的多个实例对象

运行实例 »

点击 "运行实例" 按钮查看在线实例

注册树模式 / 对象池模式


实例

<?php

/医院
 *  注册树模式 / 对象池模式
 */
class Demo1
{
    public function show()
    {
        echo 'this is demo1';
    }
}

class Demo2
{
    public function show()
    {
        echo 'this is demo2';
    }
}

class Demo3
{
    public function show()
    {
        echo 'this is demo3';
    }
}

class Tree
{
    private static $obj = [];

    // 将对象挂载到注册树上
    public static function set($key, $val)
    {
        self::$obj[$key] = $val;
    }

    // 将对象从注册树上取出来
    public static function get($key)
    {
        return self::$obj[$key];
    }

    // 将对象从注册树上删除
    public static function del($index)
    {
        unset(self::$obj[$index]);
    }
}

Tree::set('demo1',new Demo1());
Tree::set('demo2',new Demo2());
Tree::set('demo3',new Demo3());
echo '<hr>';
$obj1 = Tree::get('demo1');
$obj1->show(); // this is demo1
echo '<hr>';
$obj2 = Tree::get('demo2');
$obj2->show(); // this is demo2
echo '<hr>';
$obj3 = Tree::get('demo3');
$obj3->show(); // this is demo3

// 总结:  注册树模式就是直接用数组来存储对象

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!