Home >Backend Development >PHP Tutorial >Detailed explanation of singleton pattern in PHP implementation design pattern, detailed explanation of PHP design pattern_PHP tutorial

Detailed explanation of singleton pattern in PHP implementation design pattern, detailed explanation of PHP design pattern_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:29850browse

Detailed explanation of singleton pattern in php implementation design pattern, detailed explanation of php design pattern

【Summary】

Ensure that a class has only one instance and provide a global access point to access it [GOF95]

【Features】

1. A class has only one instance
2. It must create this instance by itself
3. You must provide this instance to the entire system yourself

【Structural Diagram】

【Main Character】

Singleton defines an Instance operation, allowing clients to access its only instance. Instance is a class method. Responsible for creating its only instance.

【Advantages and Disadvantages】

1. Controlled access to unique instance
2. Reduce the namespace. The singleton mode is an improvement on global variables. It avoids polluting the namespace with global variables that store unique instances
3. Allows for the essence of operation and presentation. Singleton classes can have subclasses. And it is very easy to configure an application with an instance of this extended class. You can configure your application at runtime with instances of the classes you need.
4. Allow a variable number of instances (multiple instance mode)
5. More flexible than class operations

【Applicability】

1. When a class can only have one instance and clients can access it from a well-known access point
2. When this only instance should be extensible through subclassing. And users should be able to use an extended instance without changing their code.

[Singleton mode php instance]

Copy code The code is as follows:

/**
* Singleton mode
*-------------
* @author zhaoxuejie
* @package design pattern
* @version v1.0 2011-12-14
​*/
class Singleton {

//Private static member variables, save global instances
private static $instance = NULL;

//Private construction method to ensure that the outside world cannot directly instantiate
private function __construct(){}

//Static method, returns the only instance of this type
public static function getInstance(){
if(!isset(self::$instance)){
$c = __CLASS__;
Self::$instance = new $c;
}
Return self::$instance;
}

//Method for testing
public function info(){
return 'ok';
}

//Prevent cloning
public function __clone(){
trigger_error('Clone is not allowed.', E_USER_ERROR);
}
}

$s = Singleton::getInstance();
echo $s->info();
?>

php design pattern: Write PHP5 sample code for factory mode and monomorphic mode

Example #1 Call factory method (with parameters)

class Example
{
// The parameterized factory method
public static function factory($ type)
{
if (include_once 'Drivers/' . $type . '.php') {
$classname = 'Driver_' . $type;
return new $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
?>
------------ --------------------------
Example #2 Singleton pattern

class Example
{
// Save the class instance in this attribute
private static $instance;

// The constructor is declared private to prevent direct creation of objects
private function __construct()
{
echo 'I am constructed';
}

// singleton method
public static function singleton()
{
if (!isset(self:: $instance)) {
$c = __CLASS__;
self::$instance = new $c;
}

return self::$instance;
}

// Normal method in Example class
public function bark()
{
echo 'Woof!';
}

// Prevent users from copying object instances
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}

}

?>

Monomorphic Design Pattern in PHP

When it comes to Java, programmers say it is a singleton design pattern. In PHP, it is usually called a singleton pattern. The term is different. It is also introduced in the manual:

Singleton is used to generate a class A unique object. The most commonly used place is database connection. After using the singleton pattern to generate an object, the object can be used by many other objects.
class Example
{
// Save the class instance in this attribute
private static $instance;

// The constructor is declared private, Prevent direct creation of objects
private function __construct()
{
echo 'I am constructed';
}

// singleton method
public static function singleton()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}

return self::$instance;
}

// Ordinary method in Example class
public function bark()
{
echo 'Woof!';
}

// Prevent users from copying object instances
public function __clone()
{
trigger_error('Clone is not allowed.', E_USER_ERROR);
}

}

?>

In this way we can get a unique object of the Example class.

// This way of writing will go wrong because the constructor is declared as private
$test = new Example;

// The following will get the Example class Singleton object
$test = Example::singleton();
$test->bark();

// Copying the object will result in an E_USER_ERROR.
$test_clone = clone $test;
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/892261.htmlTechArticleDetailed explanation of the singleton pattern in PHP implementation design pattern, Detailed explanation of PHP design pattern [Summary] Ensure that there is only one class instance, and provides a global access point to access it [GOF95] [Special...
Statement:
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