PHP设计模式之工厂/单例/注册者模式

本文深入探讨了工厂模式、单例模式和注册器模式的实现原理与应用案例,通过PHP代码详细解析了各种模式的结构与功能,为读者提供了丰富的设计模式实践指导。

工厂模式

简单工厂模式 【静态工厂方法模式】(Static Factory Method)
是类的创建模式

工厂模式的几种形态:

1、简单工厂模式(Simple Factory)又叫做 静态工厂方法模式(Static Factory Method)

2、工厂方法模式(Factory Method)又叫做 多态性工厂模式(Polymorphic Factory)

3、抽象工厂模式(Abstract Factory)又叫做 工具箱模式(ToolKit)

创建工厂类

<?php
require_once ("test.php");

class Factory
{
public static function createObj()
{
$obj = new test();
return $obj;
}
}

调用工厂类中的静态方法创建对象

<?php
require_once ("Factory.php");

//$test = new test();
$test = Factory::createObj();
echo $test->name;

单例模式

创建类的唯一实例

test.php

<?php

class test
{
private static $_instance = null;
private function __construct()
{
echo "create obj";
}
private function __clone()
{

}
public static function getInstance()
{
if (static::$_instance === null)
{
static::$_instance = new static;  //使用static替代self,static关键字来访问静态的方法或者变量,与self不同,static的引用是由运行时决定,保证继承有效
}
return static::$_instance;
}
}

index.php

<?php
require_once ("test.php");

$obj = test::getInstance();

注册器模式

单例模式保证了一个类中只有一个实例被全局访问,当你有一组全局对象被全局访问时可能就需要用到注册者模式 (registry),它 提供了在程序中有条理的存放并管理对象 (object)一种解决方案。一个“注册模式”应该提供get() 和 set()方法来存储和取得对象(用一些属性key)而且也应该提供一个isValid()方法来确定一个给定的属 性是否已经设置。 
注册模式通过单一的全局的对象来获取对其它对象的引用

register.php 注册器读写类

<?php
class Registry extends ArrayObject
{
   private static $_instance = null;
   /**
    * 取得Registry实例
    *
    * @note 单件模式
    *
    * @return object
    */
   public static function getInstance()
   {
     if (self:: $_instance === null) {
       self:: $_instance = new self();
       echo "new register object!" ;
     }
     return self:: $_instance ;
   }
   /**
    * 保存一项内容到注册表中
    *
    * @param string $name 索引
    * @param mixed $value 数据
    *
    * @return void
    */
   public static function set( $name , $value )
   {
     self::getInstance()->offsetSet( $name , $value );
   }
   /**
    * 取得注册表中某项内容的值
    *
    * @param string $name 索引
    *
    * @return mixed
    */
   public static function get( $name )
   {
     $instance = self::getInstance();
     if (! $instance ->offsetExists( $name )) {
       return null;
     }
     return $instance ->offsetGet( $name );
   }
   /**
    * 检查一个索引是否存在
    *
    * @param string $name 索引
    *
    * @return boolean
    */
   public static function isRegistered( $name )
   {
     return self::getInstance()->offsetExists( $name );
   }
   /**
    * 删除注册表中的指定项
    *
    * @param string $name 索引
    *
    * @return void
    */
   public static function remove( $name )
   {
     self::getInstance()->offsetUnset( $name );
   }
}
test.php 需要注册的类
<?php
class Test
{
    function hello()
    {
     echo "hello world" ;
     return ;
    }
}
?>
index.php 测试类
<?php
//引入相关类
require_once "Registry.class.php" ;
require_once "test.class.php" ;
//new a object
$test = new Test();
//$test->hello();
//注册对象
Registry::set( 'testclass' , $test );
//取出对象
$t = Registry::get( 'testclass' );
//调用对象方法
$t ->hello();
?>

转载于:https://www.cnblogs.com/-simon/p/5875654.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值