码迷,mamicode.com
首页 > 编程语言 > 详细

大话设计模式_单例模式(Java代码)

时间:2014-06-09 15:07:30      阅读:376      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

单例模式:保证一个类仅有一个实例,并提供一个访问它的全局访问点。

简单描述:构造函数设置为private,以禁止外部类实现本类。本类中提供一个静态方法返回一个本例对象(注意多线程中的实现)

大话设计模式中的截图:

bubuko.com,布布扣

代码例子:

Singleton类:

bubuko.com,布布扣
 1 package com.longsheng.singleton;
 2 
 3 public class Singleton {
 4 
 5     private static Singleton instance = null;
 6     private static Object mObject = new Object();
 7 
 8     private Singleton() {
 9 
10     }
11 
12     public static Singleton getInstance() {
13         if (instance == null) {
14             synchronized (mObject) {
15                 if(instance == null) {
16                     instance = new Singleton();
17                 }
18             }
19         }
20         return instance;
21     }
22 
23 }
bubuko.com,布布扣

客户端:

bubuko.com,布布扣
 1 package com.longsheng.singleton;
 2 
 3 public class Client {
 4 
 5     public static void main(String args[]) {
 6         Singleton instance1 = Singleton.getInstance();
 7         Singleton instance2 = Singleton.getInstance();
 8         if( instance1 == instance2 ) {
 9             System.out.println("同一个实例对象");
10         } else {
11             System.out.println("不同的实例对象");
12         }
13     }
14 
15 }
bubuko.com,布布扣

运行结果:

1 同一个实例对象

一个类要想只实例化一次,最好的实现就是让类自身负责保存它的唯一实例。这个类可以能保证没有其他实例可以被创建,并且它可以提供一个访问该实例的方法。

单例模式可以严格的控制客户怎样访问它以及何时访问它,即对唯一实例的受控访问

另外一种实现方式;

OtherSingleton类;

bubuko.com,布布扣
 1 package com.longsheng.singleton;
 2 
 3 public class OtherSingleton {
 4 
 5     private static OtherSingleton mSingleton = new OtherSingleton();
 6     
 7     private OtherSingleton() {
 8         
 9     }
10     
11     public static OtherSingleton getInstance() {
12         return mSingleton;
13     }
14 }
bubuko.com,布布扣

客户端代码:

bubuko.com,布布扣
 1 package com.longsheng.singleton;
 2 
 3 public class Client {
 4 
 5     public static void main(String args[]) {
 6         /*Singleton instance1 = Singleton.getInstance();
 7         Singleton instance2 = Singleton.getInstance();
 8         if( instance1 == instance2 ) {
 9             System.out.println("同一个实例对象");
10         } else {
11             System.out.println("不同的实例对象");
12         }*/
13         
14         OtherSingleton instance1 = OtherSingleton.getInstance();
15         OtherSingleton instance2 = OtherSingleton.getInstance();
16         if( instance1 == instance2 ) {
17             System.out.println("同一个实例对象");
18         } else {
19             System.out.println("不同的实例对象");
20         }
21     }
22 
23 }
bubuko.com,布布扣

运行结果:

1 同一个实例对象

书上说OtherSingleton应该定义成不可被继承(即定义成final类),我觉得不必,子类必须调用父类的构造方法,而OtherSingleton的构造方法其他类是无法访问的,也就不能生成子类,所以不必加final关键字

大话设计模式_单例模式(Java代码),布布扣,bubuko.com

大话设计模式_单例模式(Java代码)

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/creasylai19/p/3776553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!