Learning Design Patterns

Source: Internet
Author: User
Tags php tutorial
design mode just for Java? Architect--at least you may always think so. In fact, design patterns are very useful for everyone. If these tools are not patents for "architecture astronauts", then what are they? Why they are very useful in PHP applications.

Today we will study the singleton mode:

To learn a pattern, we always have to know the application scene. Otherwise, it is like learning a martial arts moves, if you do not know under what circumstances to use, it is not wasted Kung fu ah.

For Singleton mode, one of the most common applications is database connectivity. If you open a Web page, build a link, and that kind of sales is a big waste. So we need to use a singleton pattern to make sure that the system only creates new connections when necessary.

If you are using pseudo-code to describe a singleton pattern, you can write:

if (connection exists) {

Return to this link

}else{

Create a new link

}

However, this implementation is obviously problematic, how to save the link, can be accessed externally, but also can not be destroyed by the outside. This is easy if it is implemented in PHP. Let's look at a code together:

// Get Data class class  Private$_dbprivatestatic $_instance  Private function  $DSN = '. /.. /content/xxtebook.db '$thisnew PDO (' SQLite: ').  $DSN);     // initializing a database connection  Public Static function  if(! (Self::$_instance  instanceof Self)) {            self::$_instancenew self ();         return self::$_instance;       // Other methods  }

Why do you want to privatize the constructor? This question is very good, and you are welcome to ask such questions.

First, as a singleton, it is to avoid accidental creation of new instances by outsiders. This is ensured by an internal approach to the creation of the instance. and internal methods access internal variables, with congenital advantages.

In fact, we can save the instance's node to the inside of the class so that the next time the value is checked, it can be removed and used without having to recreate it.

Of course, I saw some books that made an empty __clone () {} method to prevent cloning, which is good. The icing on the cake, I left it to the specific project to do.

Finally, let's look at how to use this single example:

$db = db:://$db->sommethod ();

Isn't it very simple.

In addition, for which methods are static, which methods are not, my idea is that if the method inside to access the $this variable, then you should not use static methods, and vice versa can be used static. In other words, static methods do not need to be used by example real objects.

Next time we'll learn the factory model together.

The above introduces the learning design pattern, including the design pattern (1) aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.