Learn design patterns with me (-), learn design patterns _ PHP tutorials

Source: Internet
Author: User
Learn design patterns with me (-) and design patterns. Learn the design mode with me (-). learn the design mode. today we will study the Singleton mode together: to learn a model, we always need to know the application scenario. Otherwise, it would be like learning a design pattern with me (-) and learning the design pattern.

Today, let's take a look at the Singleton mode:

To learn a model, we always need to know the application scenario. Otherwise, it would be like learning a martial arts trick. if you don't know under what circumstances to use it, isn't it a waste of effort.

For the Singleton mode, the most common application is database connection. If a link is built every time a webpage is opened, this consumption is a big waste. Therefore, we need to use the Singleton mode to ensure that the system establishes a new connection only when necessary.

If you use pseudocode to describe the Singleton mode, you can write it as follows:

If (connection exists ){

Return this link

} Else {

Create a new link

}

However, this implementation is obviously problematic. how can this link be saved before it can be accessed externally without being damaged by the outside. If php is used for implementation, it is very easy. Let's take a look at a piece of code:

// Get data classclass DB {private $ _ db; private static $ _ instance; private function _ construct () {$ DSN = '.. /.. /content/xxtebook. db'; $ this-> _ db = new PDO ('sqlite :'. $ DSN) ;}// initialize the database connection public static function initDB () {if (! (Self: $ _ instance instanceof self) {self: $ _ instance = new self ();} return self: $ _ instance;} // Other methods}

Why is it necessary to privatize constructors? This is a good question. You are welcome to raise this question.

First, as a singleton, it is necessary to avoid creating new instances by external accidents. You can use an internal method to create an instance. In addition, internal methods have inherent advantages in accessing internal variables.

In fact, we can save the instance to the internal class when it is completed, so that the next time we check the existence of this value, we can directly retrieve and use it without re-creating it.

Of course, I have read some books and made an empty _ clone () {} method to prevent cloning. this is good. The icing on the cake, I will leave it to a specific project.

Finally, let's see how to use this Singleton:

$db = DB::initDB();//$db->somMethod();

Is it very simple.

In addition, for which methods use static and which do not, my idea is that if the method needs to access the $ this variable internally, the static method should not be used. Otherwise, the static method can be used. That is to say, static methods can be used without passing through the instance object.

Next time we will study the factory model together

Middleware (-), learning the design model today we will learn the Singleton model together: learning a model, we always need to know the application scenario. Otherwise, it would be like learning a course...

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.