Home > Back-end >  The singleton pattern of notes
The singleton pattern of notes

Time:01-20

Design patterns: singleton
Public class Singleton
{
////not hungry hungry mode han mode
Private static Singleton Singleton=new Singleton (); Private static Singleton Singleton;
Private Singleton ()
{
}
Public static Singleton getInstance () public synchronized static Singleton getInstance ()
{{
//idlers model//double lock mechanism
If (singleton==null) if (singleton==null)
{{
The singleton=new singleton (); Synchronized (Singleton class)
} {
Return the singleton (); If (singleton==null)
} {
} the singleton=new singleton ();
}
}
}
Return the singleton.
}
//in theory, there will be no more fish
Public class InnerClassSinleton ()
{
Pricate InnnerClassSingleton ()
{
}
Private static InnerClassSinleTon ()
{
Public static final InnerClassSingleton single=new InnerClassSingleton ();
}
Public static InnerClassSingleTon getInstance ()
{
Return InnerClassSingleTon. Single;
}
}

Public class RunTest ()
{
Public static void main (String args [])
{
The Singleton s=Singleton. GetInstace ();
}
}
  • Related