PHP design mode single-instance mode

Source: Internet
Author: User

A concept

Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system. That is, a class has only one instance of an object.

The main characteristics of the main three:

1. A class can have only one object;

2. The object of this class must be created on its own;

3, to the whole system to provide this one object;

From the specific implementation point of view, is the following three points:

one is that the Singleton mode class only provides a private constructor,

The second is that the class definition contains a static private object of the class.

The third is that the class provides a static public function to create or get a static private object of its own.

Of course, there must be a private clone method to prevent cloning;

A singleton class cannot be instantiated directly in any other class, but only by its own instantiation. Instead of creating an instance copy, it returns a reference to the instance stored inside the singleton class.

Advantagesfirst, instance control Singleton mode prevents other objects from instantiating copies of their own singleton objects, ensuring that all objects have access to unique instances.

Second, flexibility

because classes control the instantiation process, classes can flexibly change the instantiation process.

Disadvantages First, the cost

Although the number is small, there will still be some overhead if you want to check for instances of the class every time the object requests a reference. This problem can be resolved by using static initialization.

Ii. Possible confusion of development

when using singleton objects, especially those defined in a class library, developers must remember that they cannot instantiate objects using the New keyword. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly.

Iii. Object Lifetime

the problem of deleting a single object cannot be resolved. In a language that provides memory management, such as a. NET framework-based language, only a singleton class can cause an instance to be deallocated because it contains a private reference to the instance. In some languages, such as C + +, other classes can delete object instances, but this results in a floating reference in a singleton class.

Why use singleton mode?

Look at a piece of code first

Each time the database connection is new to this class, then call the Mysqlconnect method, return the connection, then close the connection, frequent new and database connection close operations are very resource-intensive! Database software system in the use of database connection pool, mainly to save the open or close the database connection caused by the efficiency of loss, this loss of efficiency is very expensive, because how to use a single case mode to maintain, can greatly reduce this loss.

Therefore, in order to avoid resource consumption, we have the following improvements:

This will not have to come every time the new class, a lot of convenience. Here is a detailed:

When used:

$con = Mysql::getinstance ($host, $username, $password);
$con-select_db ($database);

Application Scenarios

Of course, the singleton pattern is not just applied to the operation class of the database. It can also be applied in these areas:

1. The website counter, generally also uses the Singleton pattern realization, otherwise difficult to synchronize.

2. Application of the log application, the general use of single-case mode implementation, which is generally due to the shared log file is open, because there can only be one instance to operate, otherwise the content is not good to append.

3. The Web application's configuration object reads, generally also applies the singleton pattern, this is because the configuration file is the shared resource.

PHP design mode single-instance mode

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.