===== 23 design patterns of (single mode)

 

 

Use enumeration codes are as follows:

Enumeration class (recommended)

 1 public enum  SomeThing {
 2 
 3     INSTANCE;
 4     private Resource instance;
 5 
 6     SomeThing() {
 7         instance = new Resource();
 8     }
 9 
10     public Resource getInstance() {
11         return instance;
12     }
13 }

Object Class

1 public class Resource {
2 }

main method

1     public static void main(String[] args) {
2         Resource instance1 = SomeThing.INSTANCE.getInstance();
3         System.out.println(instance1);
4         Resource instance = SomeThing.INSTANCE.getInstance();
5         System.out.println(instance);
6     }

Output

1 com.itheima.test.Resource@697446d4
2 com.itheima.test.Resource@697446d4

That implement internal static

code show as below

. 1  public  class SingletonTest {
 2      / ** privatization constructor * / 
. 3      Private SingletonTest () {
 . 4      }
 . 5  
. 6      / ** provide external access to common methods * / 
. 7      public  static SingletonTest the getInstance () {
 . 8          return UserSingletonHolder.INSTANCE;
 9      }
 10  
. 11      / * write a static inner class, which class is instantiated outside * / 
12 is      Private  static  class UserSingletonHolder {
 13 is          Private  static  Final SingletonTest iNSTANCE =new SingletonTest();
14     }
15 }

main method

1   public static void main(String[] args) {
2     
3         SingletonTest instance = SingletonTest.getInstance();
4         System.out.println(instance);
5         
6         SingletonTest instance1 = SingletonTest.getInstance();
7         System.out.println(instance1);
8     }

result

1 com.itheima.test.SingletonTest@1ab06251
2 com.itheima.test.SingletonTest@1ab06251

Lazy mode

code show as below

. 1  public  class SingletonTest {
 2      // . Lanhan formula singleton first call when their instantiation 
. 3      Private SingletonTest () {}
 . 4      Private  static SingletonTest SINGLE = null ;
 . 5      // static factory method 
. 6      public  static SingletonTest the getInstance () {
 . 7          IF (SINGLE == null ) {
 . 8              SINGLE = new new SingletonTest ();
 . 9              System.out.println ( "Create a" );
 10          }
 . 11          return SINGLE;
 12 is     }
13     public void show(){
14         System.out.println("我是show");
15     }
16 }

main method

. 1     public  static  void main (String [] args) {
 2          // deliberately write acquired twice, to create two objects 
. 3          SingletonTest Singleton = SingletonTest.getInstance ();
 . 4          SingletonTest singleton2 = SingletonTest.getInstance ();
 . 5  
. 6          // the Singleton objects are created only once, but twice can still write, but all methods that can be called, but see below 
7          singleton.show ();
 8          singleton2.show ();
 9  
10          // behave as two objects 
11          IF (Singleton == singleton2) {
 12 is              System.out.println ( "string representation of the object:" );
 13             System.out.println("singleton :" + singleton.toString());
14             System.out.println("singleton2:" + singleton2.toString());
15         }
16     }

result

1  created once
 2  I Show
 3  I Show
 4  string representation of the object:
 5  Singleton: com.itheima.test.SingletonTest@1ab06251
 6 singleton2: com.itheima.test.SingletonTest@1ab06251

Starving mode

code show as below

1 public class SingletonTest {
2     private SingletonTest() {}
3     private static final SingletonTest single = new SingletonTest();
4     //静态工厂方法
5     public static SingletonTest getInstance() {
6         return single;
7     }
8 }

main method

1     public static void main(String[] args) {
2        SingletonTest instance = SingletonTest.getInstance();
3         System.out.println(instance);
4 
5        SingletonTest instance1 = SingletonTest.getInstance();
6        System.out.println(instance1);
7    }

Dual detection mode lock

code show as below

 1 public class Singleton {  
 2     private volatile static Singleton singleton;  
 3     private Singleton (){}  
 4     public static Singleton getSingleton() {  
 5     if (singleton == null) {  
 6         synchronized (Singleton.class) {  
 7         if (singleton == null) {  
 8             singleton = new Singleton();  
 9         }  
10         }  
11     }  
12     return singleton;  
13     }  
14 }

 

to sum up

1 . Hungry Chinese-style 
 thread-safe, high-efficiency calls, but can not delay loading 
 
 2 . Lazy man's 
 thread-safe, call the efficiency is not high, can lazy load 
 
 3 . Double lock detection 
 because of the internal model underlying JVM, and occasionally go wrong. Not recommended 
 
 4 static inner classes style 
 thread-safe, high resource utilization, load can be delayed 
 
 5 Example enumeration single 
 thread-safe, high-efficiency calls, but can not delay loading

 

Guess you like

Origin www.cnblogs.com/monkey1024/p/11493668.html