Design Mode-singleton mode, design mode-Mode

Source: Internet
Author: User

Design Mode-singleton mode, design mode-Mode
1. Introduction

1.1 Definition

Make sure that a class has only one instance, and instantiate it and provide the instance to the entire system.

1.2 four elements of Singleton Mode

  • Private Constructor
  • Private clone method
  • Private Static reference pointing to your instance
  • Static public method returned by your instance

1.3 The Singleton mode is divided into two types based on the time of instantiating objects

One is the hunger-type Singleton, while the hunger-type Singleton is in the singleton class.When loadedTo instantiate a reference from an object;

One is the lazy Singleton, while the lazy one will instantiate the object only when calling to obtain the instance method.

1.4 advantages of Singleton Mode

  • There is only one object in the memory, saving memory space.
  • This avoids frequent creation and destruction of objects and improves performance.
  • Avoid multiple usage of shared resources.
  • Global access.

1.5 applicable scenarios

  • Objects that need to be instantiated frequently and then destroyed.
  • Objects that are frequently used when creating objects that consume too much time or resources.
  • A Tool Object with A status.
  • Objects that frequently access databases or files.
  • .....

 

2. PHP code implementation
1 <? Php 2 # Singleton Mode 3 class Singleton 4 {5 private static $ _ instance; 6 private function _ construct () {}# private constructor 7 private function _ clone () {}# private clone method 8 public static function getInstance () # static common method instantiation object 9 {10 if (! (Self: $ _ instance instanceof self) 11 {12 self: $ _ instance = new self (); # point to the Private Static reference of the instance 13} 14 return self :: $ _ instance; 15} 16 17} 18 19?>

 

3. memcached Singleton Mode

If you think that memcached can store objects, you need to store them in memcached memory. In this way, we can avoid the instantiation of every loop execution script. This will not go further on the basis of the singleton mode.

When you pull up your sleeves, let's get started. The code below is to integrate memcached into the class in singleton mode.

Note ]:

Here, memcached is integrated into the singleton mode for experimental testing only (implementing the idea in your mind). It is unknown whether the final result can be optimized.

1 <? Php 2 # Singleton mode + memcached (memory-used Singleton mode) 3 class Singleton 4 {5 private static $ _ instance; 6 private function _ construct () {} 7 private function _ clone () {} 8 public static function getInstance () 9 {10 if (! (Self: $ _ instance instanceof self) # Check whether $ _ instance has an object 11 {12 # check whether there is any such object in the memcached query. 13 $ m = new memcached (); 14 $ m-> addserver ('2017. 168.95.11 ', 11211); 15 $ singleton = $ m-> get ('singleton'); 16 if ($ singleton) #17 {18 # test --- start19 echo "memcached contains objects: <br/>"; 20 echo "<pre>"; 21 var_dump ($ singleton ); 22 echo "</pre> 

First visit:

Second access:

In memcached:

 

(The above are some of your own opinions and conclusions. If you have any shortcomings or errors, please point them out)

Author: The leaf with the wind http://www.cnblogs.com/phpstudy2015-6/

Statement: The above only represents the point of view or conclusion I have summarized at a certain time in my work and study. When reprinting, please provide the original article link clearly on the Article Page

 

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.