设计模式(十三)——单件模式

1.描述

保证一个类仅有一个实例,并提供一个访问他的全局访问点。

2.模式的使用

·在JVM加载单件类时创建他的唯一实例。

·在单件类提供的类方法中创建这个唯一实例。

 1 package 单件模式;
 2 
 3 public class test1 {
 4 
 5     public static void main(String[] args) {
 6         Moon moon1 = Moon.getInstance();
 7         Moon moon2 = Moon.getInstance();
 8         System.out.println(moon1.hashCode() + "
" + moon2.hashCode());
 9     }
10 
11 }
12 
13 /*
14  * 在JVM加载时唯一创建
15  */
16 class Demo1{
17     private static Demo1 demo1 = new Demo1();
18     private Demo1(){}
19     public static Demo1 getInstance(){
20         return demo1;
21     }
22 }
23 
24 /*
25  * 在单件提供类方法是唯一创建
26  */
27 class Demo2{
28     private static Demo2 demo2;
29     private Demo2(){}
30     public static synchronized Demo2 getInstance(){
31         if(demo2 == null)
32             demo2 = new Demo2();
33         return demo2;
34     }
35 }
36 
37 class Moon{
38     private static Moon moon;
39     double radius, distanceToEarth;
40     private Moon(){
41         moon = this;
42         radius = 1728;
43         distanceToEarth = 363300;
44     }
45     public static synchronized Moon getInstance(){
46         if(moon == null)
47             moon = new Moon();
48         return moon;
49     }
50     
51     public String show(){
52         return "月球半径" + radius + "千米,距地球距离" + distanceToEarth + "千米";
53     }
54 }

3.使用情景

·当系统需要某个类只能有一个类。

4.优点

·单件类唯一实例由单件类本身控制,便于控制用户访问。

5.UML

没有

6案例

 多个线程执行任务,获取最先完成的线程的信息。思路是:使用单件模式设计一个End类,用最先完成的线程初始化一个end对象即可。

 1 package 单件模式;
 2 
 3 import java.util.ArrayList;
 4 
 5 public class test2 {
 6 
 7     public static void main(String[] args) {
 8         Threads t1 = new Threads();
 9         t1.setN(10000);
10         Threads t2 = new Threads();
11         t2.setN(10000);
12         Threads t3 = new Threads();
13         t3.setN(10000);
14         t1.setPriority(10);
15         t2.setPriority(5);
16         t3.setPriority(6);
17         t1.start();
18         t2.start();
19         t3.start();
20         System.out.println(End.getInstance().getMess());
21     }
22 
23 }
24 
25 /*
26  * 最先完成的线程得到该唯一End对象
27  */
28 class End{
29     private static End end;
30     String mess;
31     private End(String mess){
32         this.mess = mess;
33     }
34     
35     private End(){}
36     
37     public static synchronized End getInstance(String mess){
38         if(end == null)
39             end= new End(mess + "最先完成");
40         return end;
41     }
42     
43     public static synchronized End getInstance(){
44         if(end == null)
45             end= new End();
46         return end;
47     }
48     
49     public static void initEnd(){
50         end = null;
51     }
52     
53     public String getMess(){
54         return mess;
55     }
56 }
57 
58 /*
59  * 线程
60  */
61 class Threads extends Thread{
62     int n;
63     End end;
64 
65     public void setN(int n) {
66         this.n = n;
67     }
68     
69     public void run() {
70          synchronized (this){
71              while(n > 0){
72                     n--;
73                 }
74             End end = End.getInstance(this.getName());
75          }
76     }    
77 }

有时候结果为NULL,我还得再研究一下怎么回事。

原文地址:https://www.cnblogs.com/cxy2016/p/7670260.html