Design Mode-singleton mode (Java & amp; PHP)

Source: Internet
Author: User

Design Mode-singleton mode (Java & PHP)
In simple terms, the singleton mode ensures that only one class instance exists throughout the application. It is like an application in Java Web, which provides a global variable and is widely used, such as saving global data and implementing global operations. The simplest implementation that can be thought of is to write the class constructor as private, so that other classes cannot be instantiated, then a static instance is provided in the class and can be returned to the user. In this way, you can use this reference to use the instance of this class. The Singleton mode introduces two types: hunger and lazy. Copy the code package com. yydcdut. singleton1; public class Single2 {private Single2 () {}; private static Single2 instance = new Single2 (); public static Single2 getInstance () {return instance ;}} copying code this method is based on the classloder mechanism to avoid multi-thread synchronization issues. However, instances are instantiated during class loading. Although there are many causes for class loading, in Singleton mode, most of them call the getInstance method, but it cannot be determined that other methods (or other static methods) cause class loading, at this time, the initialization of the instance obviously does not achieve the lazy loading effect. The code package com. yydcdut. singleton1; public class Single1 {private Single1 () {}; private static Single1 instance = null; public static Single1 getInstance () {if (instance = null) return instance = new Single1 (); else return instance;} copy the code. lazy loading is obvious, but it is fatal that it cannot work normally in multiple threads. Test the copy code package com. yydcdut. singleton1; public class SingleMain {public static void main (String [] args) {Single1 single1 = Single1.getInstance (); Single1 single2 = Single1.getInstance (); System. out. println (single1.toString (); System. out. println (single2.toString (); System. out. println (single1 = single2); System. out. println ("======================"); Single2 single21 = Single2.getInstance (); Single2 Single22 = Single2.getInstance (); System. out. println (single21.toString (); System. out. println (single22.toString (); System. out. println (single21 = single22);} copy code PHP Singleton mode copy Code <? Php class DB {private $ link; private static $ instance = null; private function _ construct () {$ this-> link = mysql_connect ('localhost', 'root ', ''); mysql_select_db ('base', $ this-> link);} static function getInstance () {if (is_null (self ::$ instance) {self :: $ instance = new self;} return self: $ instance;} function select () {$ SQL = "select * from factory"; $ result = mysql_query ($ SQL ); if ($ result & mysql_num_rows ($ Result)> 0) {while ($ arr = mysql_fetch_assoc ($ result) {$ data [] = $ arr;} return $ data ;} else {return false ;}}$ db = DB: getInstance (); $ db = DB: getInstance (); $ db = DB: getInstance (); $ arr = $ db-> select (); print_r ($ arr);?> The single example of copying code PHP is mainly used for databases, statistics on the number of visitors, and so on. The design mode of PHP is less for Java, because PHP is too flexible. For example, PHP does not have reflection, PHP can directly get a String type to get new, and my friends and I are stunned. The flexibility makes me sometimes messy.

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.