PHP design mode-Single case mode learning notes

Source: Internet
Author: User

Profile

Create pattern
Guarantees that a class has only one instance and provides a global access point to access it

Characteristics

1, a class has only one instance
2. It must create this instance on its own
3. This example must be provided to the whole system by itself

Structure diagram

Main roles

Singleton defines a instance operation that allows the client to access its unique instance. Instance is a class method. The only instance that is responsible for creating it.

Disadvantages

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

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 through subclasses. And the user should be able to use an extended instance without changing the code.

Single Case mode PHP instance

The code is as follows Copy Code

<?php
/**
* Single case mode
* ————-
* @author Zhaoxuejie <zxj198468@gmail.com>
* @package Design
* @version v1.0 2011-12-14
*/
Class Singleton {

Private static member variable, saving global instance
private static $instance = NULL;

Private construction method to ensure no direct instantiation of the outside world
Private Function __construct () {}

static method, which 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 method
Public Function info () {
return ' OK ';
}

Prevent cloning
Public Function __clone () {
throw new Exception (' Error:clone is not allowed. ');
}
}
$s = singleton::getinstance ();
echo $s->info ();
?>

Supplement I see another article on the introduction of the single case model

The code is as follows Copy Code

<?php
Class easyframework_easy_mysql{
protected static $_instance = NULL;
Private Function __construct () {

}
public static function getinstance () {
if (self::$_instance = = null) {
Self::$_instance = new self ();
}
return self::$_instance;
}

protected function __clone () {

}

}
$x = Easyframework_easy_mysql::getinstance ();
Var_dump ($x);

?>

/*
* 1. First step:
* Since it is a singleton, that is, it can only be instantiated once, which means that when instantiated
* It is impossible to use the NEW keyword!!!!
* The constructor in the class is invoked automatically when the new keyword is used.
* However, if we set the access control character of the constructor to protected or private
* Then it's impossible to use the NEW keyword directly!!!
* Second Step:
* Regardless of protected/private modified properties or methods, please ask in the current class of
* Is there any access to the inside? ---> Can
* Step Three:
* Now we have no way to get the object (because you can't use the new keyword),
* Step Fourth: Static members (including properties or methods) can only be accessed through the
* Class Name:: Properties ()
* Class Name:: Method ()
* Fifth Step: If I now exist a static method--> getinstance ()
* Then the call should be written as
* $object = Easyframework_easy_mysql::getinstance ()
* If the getinstance () method can get a unique object
* Also represents the so-called single case mode of!!!
* Sixth step, how to let Getinstace () only to one object?
* If you want to get the object, then it must be:
* $variabl = new???? ();
* We also know that the value of a static property is a!!! that can be inherited by all objects.
* Static members belong to the class, not the Object!
So
* Step seventh: Declare a static property with which to store the instantiated object
* PROTECTD Static $_instance
*
* and the initial value is null
* Then when I call the getinstance () method, I just need to determine if the value is empty.
*
* public static function getinstance () {
* if (self::_instance = = null) {
* Self::_instance = new self ();
*  }
* Return self::_instance;
* }
* In instances, it must be written like this:
* $x = Easyframework_easy_mysql::getinstance ();
* At the first call, the $_instance of the class is null, and the value of the static property
* So it also means that the getinstance () method of judgment is true,
* And it means
* Self::$_instance This member has a value!!!
* and also returns this value
* $y = Easyframework_easy_mysql::getinstance ();
* In the second or Nth call, the self::$_instance already has a value
* Also on behalf of getinstance () The condition of the method is FALSE!!!
* It also means that the program representatives are unlikely to perform the!!!
* Also means that will return directly to the previous value of the!!!
*
*
*
* */

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.