Singleton_單列模式案例分析與詳解

來源:互聯網
上載者:User

今天在這裡我淺談一下關於設計模式作為我們開發與設計的技術人員來說的重要性與該去怎麼理解等,準確的來說,設計模式是一種概念性的行為,是一種對物實的抽象,它所包括的不僅僅是只針對某個領域,它針對的是整個生態的存在而設計的行為,所以在這裡我只能針對我個人在編程過程中所需要的實際情況來進行講解與舉出案列來進行分析,在我之前開發的過程中常常忽視設計模式的存在,然而在今天我不得不重新關注它所可能有的魅力,因為很簡單的問題,它讓我在設計程式時會更有規範性與邏輯性,讓我們對其設計出來的程式更易於維護與擴充管理,由於今天是本人第一次在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圖:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.