PHP design pattern in the implementation of a single-case model, the PHP model of the detailed
Summary
Ensure that a class has only one instance and provides a global access point "GOF95" to access it
Characteristics
1. Only one instance of a class
2. It must create this instance on its own
3. This instance must be provided to the entire system on its own
"Structure Diagram"
"Primary Role"
Singleton defines a instance operation that allows a client to access its only instance. Instance is a class method. The only instance responsible for creating it.
Advantages and Disadvantages
1. Controlled access to the unique instance
2. Narrowing the namespace singleton mode is an improvement on global variables. It avoids those global variables that store unique instances. Pollution namespaces
3, allow the operation and expression of the essence of the Singleton class can have subclasses. It is also easy to configure an application with an instance of this extension class. You can configure your app at run time with an instance of the class you need.
4. Allow variable number of instances (multi-case mode)
5. More flexible than class operation
Applicability
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 by subclasses. And the user should be able to use an extended instance without changing the code.
"Singleton mode PHP instance"
Copy the Code code as follows:
<?php
/**
* Single case mode
* -------------
* @author Zhaoxuejie
* @package Design pattern
* @version v1.0 2011-12-14
*/
Class Singleton {
Private static member variable, saving global instance
private static $instance = NULL;
Private construction method to ensure that the outside world cannot be instantiated directly
Private Function __construct () {}
Static method that returns a unique instance of this class
public static function getinstance () {
if (!isset (self:: $instance)) {
$c = __class__;
Self:: $instance = new $c;
}
Return self:: $instance;
}
Test methods
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 the PHP5 sample code for factory and single 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 Single case mode
Class Example
{
Save the class instance in this property
private static $instance;
The construction method is declared private, preventing the object from being created directly
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;
}
Common methods in the example class
Public Function bark ()
{
Echo ' woof! ';
}
Prevent users from replicating object instances
Public Function __clone ()
{
Trigger_error (' Clone is not allowed. ', e_user_error);
}
}
?>
PHP's single-state design mode
For Java to turn around the programmer said is a single-state design mode, in PHP is usually said to be a singleton mode, the statement is not the same, the manual is also described:
Singleton mode (Singleton) is used to generate a unique object for a class. The most common place is the database connection. Once an object is generated using singleton mode, the object can be used by many other objects.
Class Example
{
Save the class instance in this property
private static $instance;
The construction method is declared private, preventing the object from being created directly
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;
}
Common methods in the example class
Public Function bark ()
{
Echo ' woof! ';
}
Prevent users from replicating object instances
Public Function __clone ()
{
Trigger_error (' Clone is not allowed. ', e_user_error);
}
}
?>
So we can get a unique object of the example class.
This is an error because the constructor method is declared as private.
$test = new Example;
The following will get the singleton object of the example class
$test = Example::singleton ();
$test->bark ();
Copying an object will result in a e_user_error.
$test _clone = Clone $test;
?>
http://www.bkjia.com/PHPjc/892261.html www.bkjia.com true http://www.bkjia.com/PHPjc/892261.html techarticle PHP Implementation of the design pattern in the single-instance mode, the PHP design pattern of "summary" to ensure that a class has only one example, and provide a access to its global access point "GOF95" "Special ...