Implementation Method of singleton mode in Python, pythonsingleton
Many developers have considered implementing the singleton mode like c ++ when learning Python, but later they will find that c ++ is c ++ and Python is Python, it cannot be simply imitated.
A common method in Python is to use global variables or class variables to implement a single piece. This article describes how to use decorator to implement the singleton mode. The sample code is as follows:
# ----------------------- Code begin --------------------- #-*-coding: UTF-8-*-def singleton (cls): "" Define a class with a singleton instance. "instances = {} def getinstance (* args, ** kwds): return instances. setdefault (cls, cls (* args, ** kwds) return getinstance #1 Class Foo (object) can be used in future Python versions when class Decorator is supported ): def _ init _ (self, attr = 1): self. attr = attrFoo = singleton (Foo) #2 2.5 and earlier versions do not support Class Decorator. if _ name _ = "_ main __": ins1 = Foo (2) # equivalent to: ins1 = singleton (Foo) (2) print "Foo (2)-> id (ins) = % d, ins. attr = % d, % s "% (id (ins1), ins1.attr, ('error', 'OK') [ins1.attr = 2]) ins2 = Foo (3) print "Foo (3)-> id (ins) = % d, ins. attr = % d, % s "% (id (ins2), ins2.attr, ('error', 'OK') [ins2.attr = 2]) ins2.attr = 5 print" ins. attr = 5-> ins. attr = % d, % s "% (ins2.attr, ('error', 'OK') [ins2.attr = 5]) # ---------------------- code end ------------------------
Output:
Foo(2) -> id(ins)=19295376, ins.attr=2, okFoo(3) -> id(ins)=19295376, ins.attr=2, okins.attr=5 -> ins.attr=5, ok
How to Implement the singleton mode in java
First, ensure that there is a unique constructor in the class, which is private and private, that is, it is not accessible except in other places of the class.
Second, there is a private variable of this type. This variable can be instantiated when declared, or it can be instantiated when called in the constructor. However, when instantiating the constructor, you must first judge:
Private A;
Public (){
If (a = null)
A = new ();
}
In this way, only one instance of A can be guaranteed,
The most important thing is to have A public method to return A type variable to return this unique variable.
Public A returnA (){
Return;
}
In this way, there are three aspects:
1. A private constructor
2. A private variable of this type
3. There must be a common method of returning the type of this class to return this unique variable.
For the singleton mode in JAVA
Package test;
Public class Singleton {
Private Singleton s;
Private Singleton ()
{
}
Public static Singleton getSigleton ()
{
If (s = null) s = new Singleton ();
Return s;
}
}
This is a Singleton mode. I don't think I need to comment it out. The principle is that the constructor method of this class is private. If it is not called outside, it cannot be new Singleton, so
If you want to get an instance, you have to call its static method getSigleton (); that is, Singleton. getSigleton (); returns a Singleton instance. Pay attention to the statements in this method, that is, if you call this method for the first time, it will give you a new instance, this instance will be called later, that is, there will be only one Singleton instance from the consumer, which is the Singleton mode.