1. What is a single case pattern.
A single example pattern is a design pattern in which a class has only one object instance in the entire application.
2. Why use a single case pattern.
Reference:
1.lamp Brother's article: http://php.lampbrother.net/html/70-1/1121.htm
2.phppan Blog: http://www.phppan.com/2010/06/php-design-pattern-6-singleton/
One of the main applications of PHP is the application scene with the database, so a large number of database operations in an application, such as a database handle to connect to the database, the use of a single example mode to avoid a large number of new operations, Because each new operation consumes memory resources and system resources .
"Advantages of the single case model"
1. Controlled access to a unique instance
2. Narrowing the single case pattern of namespaces is an improvement on global variables. It avoids the global variable pollution namespaces that store unique instances
3. Allow for operation and presentation of the essence of a single instance class can have subclasses. It is also easy to configure an application with an instance of this extended class. You can configure the application at run time using an instance of the class you need.
4. Allow variable number of instances (multiple case mode)
5, more flexible than class operation
"Single case mode applicable scenario"
1. When a class can have only one instance and the customer can access it from a well-known access point
2, when this unique instance should be extensible through subclasses. And the user should be able to use an extended instance without changing the code.
"Single case mode and other modes"
Factory approach Mode (Factory method mode): Singleton mode uses Factory mode to provide its own instance.
Abstract Factory Pattern: Abstract Factory mode factory can use a single example pattern to design a specific factory class as a single instance class.
Builder model (Builder model): Construction patterns can be designed to be a single case model of specific construction classes
3. Single Case Mode example
3.1 test.php
<?php
class test{public
$service;
Static production variables save global instance
private static $instance = null;
Connect database with mysqli
private static $SDB _user = "Test";
private static $SDB _dbname= "test";
private static $SDB _host = ' localhost ';
private static $SDB _pass = ' Ddddyq3dddduxey ';
Privatisation constructor to prevent external instantiation of object
Private function __construct () {
$this->service = new mysqli (self:: $SDB _host, Self:: $SDB _user, Self:: $SDB _pass, Self:: $SDB _dbname);
Privatization cloning function to prevent external cloning object
Private Function __clone () {
}
//static method, single instance uniform access to portal public
static function GetInstance () {
if!isset (self:: $instance) | | is_null (self:: $instance)) {
self:: $instance = new Test ();
} Return
self:: $instance;
}
Test method: Print Hello,world public
function SayHello () {
echo ' Hello,world ';
}
}
3.2 data.php
<?php
require_once (' test.php ');
Class Testobject {
protected $test;
function __construct ($id =null) {
$this->test = Test::getinstance ();
$this->test->sayhello ();
}
$obj = new Testobject ();
Run data.php, the result:
The core points of 4.PHP single example mode implementation are as follows: three
1. A static member variable (usually a $instance private variable) that holds a unique instance of the class is required.
2. Constructors and cloning functions must be declared private, in order to prevent the external program new class from losing the meaning of a single case pattern
3. A public static method (usually the GetInstance method) that accesses this instance must be provided to return a reference to a unique instance
Disadvantages of 5.PHP single case mode
As we all know, the PHP language is an interpreted scripting language, which enables each PHP page to be interpreted and executed, and all related resources are recycled. In other words, PHP does not have the language level to make an object resident memory, which is different from ASP.net, Java, such as a single meeting in Java throughout the lifecycle of the application, variables are across the page level, The uniqueness of this instance in the life cycle of the application is truly achievable. However, in PHP, all variables, whether global variables or static members of the class, are page-level, each time the page is executed, the new object will be created, will be emptied after the completion of the page, so it seems that the PHP single example mode has no meaning, So PHP Single example mode I think it makes sense to only have multiple scenarios when it comes to a single page-level request and need to share the same object resource.