您的位置:首页 > 其它

Singleton_单列模式案例分析与详解

2011-04-04 16:02 447 查看
今天在这里我浅谈一下关于设计模式作为我们开发与设计的技术人员来说的重要性与该去怎么理解等,准确的来说,设计模式是一种概念性的行为,是一种对物实的抽象,它所包括的不仅仅是只针对某个领域,它针对的是整个生态的存在而设计的行为,所以在这里我只能针对我个人在编程过程中所需要的实际情况来进行讲解与举出案列来进行分析,在我之前开发的过程中常常忽视设计模式的存在,然而在今天我不得不重新关注它所可能有的魅力,因为很简单的问题,它让我在设计程序时会更有规范性与逻辑性,让我们对其设计出来的程序更易于维护与扩展管理,由于今天是本人第一次在BLOG上写设计模式有关的问题,再说本人在ANDROID项目前期都是做的J2EE方面的哦工作,所以在这里我就以大家最常见的一个单列模式为例子进行详细讲解,然而我对其讲解之前,首先要知道单列模式的基本概念是什么:即一个类只有一个实例,并提供一个对应实例访问的全局入口点,OK,下面我就直接把我测试的代码粘贴在下面吧:

package com.jsd.pattern;
/**
* 单列模式详解
* @author jiangshide
*
*/
public class Singleton {

public static void main(String[] args) {
System.out.println("----------------单列早期设计模式实现数组冒泡排序--------------");
String str = "jiangshide";
//注意在使用二分查找时请先进行排序
int arr[] = new int[]{4,3,2,1,5,6,9,3,2,67,332,234,3,23};
int index = 9;
//测试SingletonBeforeInstanctiated
SingletonBeforeInstantiated sbi = SingletonBeforeInstantiated.getInstance();
int[] result = sbi.SingletonBeforeInstantiatedMethod1(str, arr);
System.out.println("数组排序前:");
for(int x=0;x<arr.length;x++){
System.out.print(arr[x]+"/t");
}
System.out.println();
System.out.println("数组排序后:");
for (int i = 0; i < result.length; i++) {
System.out.println(i+":"+result[i]+"/t");
}
System.out.println("----------------以上为排序--------------");

System.out.println("----------------单列直接加锁设计模式实现二分法查找--------------");
SingletonDirectlyUnlock sdu = SingletonDirectlyUnlock.getInstance();
int result_index = 0;
if(sdu != null){
result_index = sdu.getIndex(str, result, index);
System.out.println("所需查找的数字为:"+index+",查找的结果其数组下标为:"+result_index);
}else{
System.out.println("实例化为空值!");
}
System.out.println("-------------以上实现查找---------------");

System.out.println("------------------单列双重加锁设计模式实现任意数字倒计时---------------------------------");
SingletonDoubleDirectlyUnlock sddu = SingletonDoubleDirectlyUnlock.getInstance();
if(sddu != null){
sddu.getNumber(str, result_index);
}
}
}

/**
* 单列模式 :早期实例化~即为饿汉模式
*/
class SingletonBeforeInstantiated{
//类自我静态私有实例化
private static SingletonBeforeInstantiated instance = new SingletonBeforeInstantiated();
//构造方法私有化
private SingletonBeforeInstantiated(){
System.out.println("SingletonBeforeInstantiated:"+SingletonBeforeInstantiated.class);
}
//为该类设置一太个公开的访问接口为净静态获取实例化的方法
public static SingletonBeforeInstantiated getInstance(){
System.out.println("单列模式_早期实例化设计:获取访问入口点:");
return instance;
}
/**
* 一个在单列模式中所需访问的方法:处理排序
*/
int[] SingletonBeforeInstantiatedMethod1(String str,int[] arr){
if(str!=null){
System.out.println("排序标志字符为:"+str);
for (int i = 0; i < arr.length; i++) {
for (int j = arr.length-1; j > i; j--) {
if(arr[j-1] > arr[j]){
int temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
return null;
}
}

/**
* 单列模式:直接加锁设计~饱汉模式:注意有在IF判断处没有进行同步,因此在多线程环境下可能产生多个实例的问题

* 然而如果加上同步控制,在get实例时每次都要进行同步,这对性能是有严重影响的,不推介使用
*/
class SingletonDirectlyUnlock{
private static SingletonDirectlyUnlock instance = null;
private SingletonDirectlyUnlock(){
System.out.println("SingletonDirectlyUnlock:"+SingletonDirectlyUnlock.class);
}
public static synchronized SingletonDirectlyUnlock getInstance(){
System.out.println("单列模式_直接加锁实例化设计:获取访问入口点:");
if(instance == null){
instance = new SingletonDirectlyUnlock();
}
return instance;
}
/**
* 一个单列模式所需访问的方法:使用二分法查找
* @return
*/
public int getIndex(String str,int[] arr,int index){
if(str != null){
System.out.println("二分法查找标志符为:"+str);
int low = 0;
int height = arr.length;
int meddile;
while(low <= height){
meddile = (low + height) / 2;
if(index == arr[meddile]){
return meddile;
}
if(index > arr[meddile]){
low = meddile + 1;
}
if(index < arr[meddile]){
height = meddile - 1;
}
}
}
return -1;
}

//异常处理
private Throwable getThrows(){
return new Throwable("单列实例为空值!");
}
}

/**
* 单列模式:双重加锁模式:这是解决前面两种说可能存在的问题,JDK1.5以后双检锁是有效的
*/
class SingletonDoubleDirectlyUnlock{
private static SingletonDoubleDirectlyUnlock instance = null;
private SingletonDoubleDirectlyUnlock(){
System.out.println("SingletonDirectlyUnlock:"+SingletonDirectlyUnlock.class);
}
public static SingletonDoubleDirectlyUnlock getInstance(){
if(instance == null){
synchronized(SingletonDoubleDirectlyUnlock.class){

if(instance == null){
instance = new SingletonDoubleDirectlyUnlock();

}
}
}
return instance;
}
/**
* 一个单列模式所需访问的方法:实现任意数字倒计时显示效果
*/
public int getNumber(String str,int number){
if(str != null){
boolean flag = true;
System.out.println("倒计时实现查找标志符为:"+str);
System.out.print("还剩下数字为:");
jsd: while( flag){
number--;
try {
Thread.sleep(1000);
System.out.print(number);
if(number < 1){
break jsd;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return number;
}
return new Integer("标志符为空!");
}
}

下面就是其UML图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: