Home Backend Development PHP Tutorial Application Analysis of Single Case and Multiple Case Design Patterns in PHP_PHP Tutorial

Application Analysis of Single Case and Multiple Case Design Patterns in PHP_PHP Tutorial

Jul 21, 2016 pm 03:01 PM
php analyze Singleton and application model of Design Patterns

The Singleton pattern and the less common Multiton pattern control the number of classes in an application. As in the pattern name, the singleton can only be instantiated once and has only one object, while the multi-instance pattern can be instantiated multiple times.

Based on the characteristics of Singleton, we often use Singleton to configure applications and define variables that may be accessed at any time in the application. But sometimes Singleton is not recommended because it generates a global state and

This single root object does not encapsulate any system functionality. In many cases, this makes unit testing and debugging difficult. Readers make their own decisions based on circumstances.
Code example:

Copy code The code is as follows:

<?php
class SingletonExample{
private function __construct(){}//Prevent direct instantiation
public static function getInstance(){ //Not associated with any object
static $instance=null; //Call All codes of this function share this variable, there is no need to make it a static variable of the class
if($instance==null){
$instance=new SingletonExample();
}
return $ instance;
}
}
$obj1=SingletonExample::getInstance();
$obj2=SingletonExample::getInstance();
var_dump($obj1===$obj2); // true is the same instance
?>

Multiton is similar to singleton, except that the latter requires the getInstance() function to pass the key value.
There will only be a unique object instance for a given key value. If there are multiple nodes, each node has a unique identifier, and each node may appear in a single execution (such as nodes in cms) Multiple times, then you can use Multiton mode to implement these nodes. Multiton saves memory and ensures that multiple instances of the same object do not conflict.
Example:
Copy code The code is as follows:

<?php
class MultitonExample{
private function __construct(){}//Prevent direct instantiation

public static function getInstance($key){
static $instance=array();
if(!array_key_exists($key,$instance)){
$instance[$key ]=new SingletonExample();
}
return $instance($key);
}
};
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327983.htmlTechArticleThe Singleton pattern and the uncommon Multiton pattern control the number of classes in the application . Such as the pattern name, a singleton can only be instantiated once, there is only one object, the multiple instance pattern can...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles