The Singleton mode and the php design mode are described in detail. The Singleton mode in php implementation design mode is described in detail, and the php design mode is described in detail. [Overview] ensure that only one instance is available for one class, and provide a global access point to access it [GOF95] [detailed explanation of the Singleton mode in the php implementation design mode, and detailed explanation of the php design mode
Summary]
Ensure that a class has only one instance and provides a global access point to it [GOF95]
[Features]
1. a class has only one instance.
2. it must create the instance on its own.
3. you must provide this instance to the entire system.
[Structure Diagram]
[Main role]
Singleton defines an Instance operation that allows the customer to access its unique Instance. Instance is a class method. Creates a unique instance.
[Advantages and disadvantages]
1. controlled access to a unique instance
2. narrowing down the namespace Singleton mode is an improvement for global variables. It avoids the global variables that store unique instances from polluting the namespace.
3. a subclass can be added to the list class of operations and representations. In addition, it is easy to configure an application using this extension class instance. You can use the instance of the class you need to configure the application at runtime.
4. allow variable target instances (multi-sample mode)
5. more flexible than class operations
[Applicability]
1. when the class can only have one instance and the customer can access it from a well-known access point
2. this unique instance should be extensible through subclass. And the user should be able to use an extended instance without changing the code.
[Single-instance mode php instance]
The code is as follows:
<? Php
/**
* Singleton mode
*-------------
* @ Author zhaoxuejie
* @ Package design pattern
* @ Version v1.0 2011-12-14
*/
Class Singleton {
// Private static member variables to save global instances
Private static $ instance = NULL;
// Private constructor to ensure that external entities cannot be directly instantiated
Private function _ construct (){}
// Static method, which returns a unique instance of this type
Public static function getInstance (){
If (! Isset (self: $ instance )){
$ C = _ CLASS __;
Self: $ instance = new $ c;
}
Return self: $ instance;
}
// Test method
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 mode: write the PHP5 sample code of the factory mode and single-state mode.
Example #1 call the 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 mode
Class Example
{
// Save the class instance in this attribute
Private static $ instance;
// The constructor is declared as private to prevent direct object creation.
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! ';
}
// Prevents users from copying object instances
Public function _ clone ()
{
Trigger_error ('Clone is not allowed. ', E_USER_ERROR );
}
}
?>
PHP single-state design mode
For java programmers, it is a single-state design mode. in PHP, it is usually a single-instance mode. different arguments are also described in the manual:
Singleton is used to generate a unique object for a class. Database Connection is the most commonly used place. After an object is generated in Singleton mode, 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 as private to prevent direct object creation.
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! ';
}
// Prevents 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 Example class object.
// This write method will fail because the constructor is declared as private.
$ Test = new Example;
// The following is a singleton object of the Example class.
$ Test = Example: singleton ();
$ Test-> bark ();
// Copying an object will result in an E_USER_ERROR.
$ Test_clone = clone $ test;
?>
Summary: ensure that a class has only one instance, and provide a global access point [GOF95] [special...