Copy Code code as follows:
/**
* Single case mode
*
* Ensure that a class has only one instance and provides a global access point to access it
*
*/
Class Singleton
{
static private $_instance = NULL;
Private Function __construct ()
{
}
static public Function getinstance ()
{
if (Is_null (self::$_instance)) {
Self::$_instance = new Singleton ();
}
return self::$_instance;
}
Public Function display ()
that holds a unique instance of the class
private static $_instance;
2. Constructors and clone functions must be declared private to prevent the external program new class from losing the meaning of a singleton pattern
Privatisation constructor method to prevent new instance private function __construct () { $this->_db = Pg_connect (' demo ');} Overwrite __clone () method, prohibit clone private function __clone () {}
3. You must provide a public static method that accesses this instance (us
Singleton Mode (Singleton): guarantees that a class has only one instance and provides a global access point to access it.Lazy Type:namespacesingletondesign{ Public classSingleton {Private StaticSingleton instance; Private Static ReadOnly ObjectSyncRoot =New Object(); PrivateSingleton () {} Public StaticSingleton getinstance () {if(instance==NULL) { Lock(syncRoot) {if(Instance = =NULL) {instance=NewSingleton (); }
What is a singleton mode: ensure that only one instance object exists for a class and provides a global access point;Application Environment: when only one object, one instance, is neededFor example: WinForm can only create one form at a time, only when one instance object is neededCode: Public classsingleton{//Determine whether the instantiation of a class is unique through a static private variablePrivate StaticSingleton MYINSTANCD;PrivateSingleton
About C + + Singleton mode m_pinstance point to the space destruction problem, m_pinstance manual destruction is often a headache problem, memory and resource leakage is not uncommon, can have a method, let the instance automatically release.The workaround is to define an internal garbage collection class and define a static member of this class in Singleton. At the end of the program, the system automatically destructors this static member, at this t
This article illustrates the basic usage of the single example mode of JS mode. Share to everyone for your reference. as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20-21
Singleton var singletontester = (function () {function singleton (options) {options = Options | | {}; THIS.name = "Singletontester"; This.pointx = Options.pointx
This paper is the second article of design pattern learning notes, which is mainly about the single case mode. This includes lazy, a hungry man, registration, and lazy-type improvements, as well as
an instance of reading propertoes configuration files. It is expected to be divided into three sections. This is the first section, first analysis of the simplest of the lazy and a hungry man-style. The
_ instance; /// /// Helper, not involved in real construction /// Private Static Object _ lockhelper = New Object (); /// /// In this case, attributes are used and parameter passing is not supported. /// You can change attributes to methods to support parameter passing. /// Public Static Singleton1 instance{ Get { // A single thread does not need to use double check If (_ Instance
1. What is a single case pattern.
A single example pattern is a design pattern in which a class has only one object instance in the entire application.
2. Why use a single case pattern.
Reference:
1.lamp Brother's article: http://php.lampbrother.net/html/70-1/1121.htm
2.phppan Blog: http://www.phppan.com/2010/06/php-design-pattern-6-singleton/
One of the main ap
The single case (Singleton) pattern in Java is a widely used design pattern. The primary role of a singleton pattern is to ensure that only one instance of a class exists in a Java program. Some managers and controllers are often designed as a single case pattern.
The singleton pattern has many advantages, it avoids the duplication of instance objects, reduces the time cost of creating objects, saves memory
The single case pattern is the simplest design pattern in the 23 design pattern, and it is also applied in the enterprise development particularly much. The advantage of Singleton mode is that there is only one instance in the project.
Features: constructor privatization, object privatization, only provides an external access interface.
Application Scenario:
1, the system needs to share resources: such a
Concept:The single case pattern in Java is a common design pattern, the single example mode is divided into three kinds: the lazy type single case, the A Hungry man type single case, the registration type single example three kind
1. Description:
Guarantees that a class has only one instance and provides a global access point to access it.
2. The single case model has 3 main features:
2.1 Singleton classes Ensure that you have only one instance.2.2 The Singleton class must create its own instance.The 2.3 Singleton class must provide a unique instance for other objects.
3. Implementation: Lazy single case class and a hungry man
Linux Changes the root password in single-user mode 1 centos boot Press any key Enter the system Configuration Interface Guide diagram, notice in " e, to edit the Commands before booting " e key to enter Grub boot system editing interface. 650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M02/A6/F2/wKioL1neC7uzviMmAAAlwg7nNcE912.png-wh_500x0-wm_ 3-wmp_4-s_805820876.png "title=" 201
The single case mode (Singleton) is also called the single State pattern, is the simplest pattern in the design pattern, even some model masters do not call it the pattern, called it a realization skill, because the design pattern pays attention to the abstract of the relation between objects, and the single case patte
definition: Ensure that an object (class) has only one instance and provides a global access point to access it;Implementation principle: The closure is used to maintain a reference to a local variable, which holds the only instance created for the first time;Mainly used for: global caching, login floating window, etc. only need a single instance of the time;
One way to implement a single case pattern for
Thread-Safe single case modeLazy mode : That is, the first time the class instance is invoked, a new instance of that class is generated, and only this instance is returned at a later time.
Locks are required to ensure thread safety: Reason: Multiple threads may enter an If statement that determines whether an instance already exists, thereby non thread safety.
Use double-check to ensure thread safety. Ho
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.