PHP design mode factory/Singleton/registrant Mode

Source: Internet
Author: User

Factory mode

Simple Factory mode "static Factory method Mode" (Static Factory methods)
is the creation mode of the class

Several forms of Factory mode:

1. Simple Factory mode (Factory) is also called Static Factory method mode (Factory)

2, factory method mode (Factory) is also known as the polymorphism of the Factory mode (polymorphic Factory)

3, abstract Factory mode (abstract Factory) is also called Toolbox mode (ToolKit)

Create a factory class

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

Class Factory
{
public static function Createobj ()
{
$obj = new test ();
return $obj;
}
}

Call a static method in a factory class to create an object

<?php
Require_once ("factory.php");

$test = new test ();
$test = Factory::createobj ();
Echo $test->name;

Single-Case mode

To create a unique instance of a class

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; Use static instead of the Self,static keyword to access static methods or variables, unlike self, static references are determined by the runtime to ensure inheritance is valid
}
return static::$_instance;
}
}

index.php

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

$obj = Test::getinstance ();

Registrar mode

The singleton mode guarantees that only one instance of a class is globally accessible, and when you have a set of global objects that are globally accessible, you may need to use the Registrant mode (registry), which provides a solution for storing and managing objects (object) methodically in the program. A "registered mode" should provide the get () and set () methods to store and get objects (with some property key) and should also provide a isvalid () method to determine whether a given property has been set.
Registration mode gets a reference to other objects through a single global object

register.php registrar read-write class

<?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 classes that need to be registered <?php class Test {     function hello()     {      echo "hello world" ;      return ;     } } ?>
index.php Test Class <?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(); ?>

PHP design mode factory/Singleton/registrant Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.