<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    積少成多

    垃圾堆

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      29 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks

    #

    工廠模式的產品角色和簡單工廠并沒有區別。變化就是在工廠角色那。將具體創建產品的工作放到子工廠進行。這樣就便于進行擴展了。
    public
     interface IProduct {
        
    public void desc();
    }

    public class ProductA implements IProduct {
        
    public void desc() {
            
    //excute;
        }
    }

    public class ProductB implements IProduct {
        
    public void desc() {
            
    //excute;
        }
    }

    public interface IFactory {
        
    public IProduct factory();
    }

    public class FactoryA implements IFactory {
        
    public IProduct factory() {
            
    return new ProductA();
        }
    }

    public class FactoryB implements IFactory {
        
    public IProduct factory() {
            
    return new ProductB();
        }
    }

    個人覺得如果工廠的factory方法如果是靜態的也可以阿。為什么不呢?但是接口是無法申明static的方法的。只能用類或抽象類來做。不知道還有沒有更好的方法。
    public abstract class IFactory {
        
    public static IProduct factory(){return null;};
    }

    public class FactoryA extends IFactory {
        
    public static IProduct factory() {
            
    return new ProductA();
        }
    }

    public class FactoryB extends IFactory {
        
    public static IProduct factory() {
            
    return new ProductB();
        }
    }


    posted @ 2011-05-27 10:53 思無 閱讀(213) | 評論 (0)編輯 收藏



    public
     interface IProduct {
        
    public void desc();
    }

    public class ProductA implements IProduct {
        
    public void desc(){
            System.out.println(
    "A");
        }
    }

    public class ProductB implements IProduct {
        
    public void desc(){
            System.out.println(
    "B");
        }
    }

    public class Factory {
        
    public static IProduct factory(String name)throws Exception{
            
    if(name.equalsIgnoreCase("A")){
                
    return new ProductA();
            }
    else if(name.equalsIgnoreCase("B")){
                
    return new ProductB();
            }
    else{
                
    throw new Exception("Don't support");
            }
        }
    }
    優點
    1. 工廠創建的類實現相同的接口。便于統一創建和返回。
    2. 對于不支持的類型,可以拋出異常。
    缺點
    1. 不支持新加類型。如果要加入新的類型。需要修改Factory類。
    posted @ 2011-05-27 10:38 思無 閱讀(201) | 評論 (0)編輯 收藏

    算法思想是每次把待排序列分成兩部分,分別對這兩部分遞歸地用歸并排序,完成后把這兩個子部分合并成一個
    序列。

    import
     java.lang.reflect.Array;
    public class MergeSorter<extends Comparable<E>> extends Sorter<E>{
        @SuppressWarnings(
    "unchecked")
        @Override
        
    public void sort(E[] array, int from, int len){
            
    if(len<=1return;
            E[] temporary
    =(E[])Array.newInstance(array[0].getClass(),len);
            merge_sort(array,from,from
    +len-1,temporary);
        }
        
    private final void merge_sort(E[] array, int from, int to,E[] temporary){
            
    if(to<=from) return;
            
    int middle=(from+to)/2;
            merge_sort(array,from,middle,temporary);
            merge_sort(array,middle
    +1,to,temporary);
            merge(array,to,middle,temporary);
        }
        
    private final void merge(E[] array, int from, int to, int middle,E[] temporary){
            
    int k=0, leftIndex=0,rightIndex=to-from;
            System.arraycopy(array,from,temporary,
    0,middle-from+1);
            
    for(int i=0;i<to-middle;i++){
                temporary[to
    -from-i]=array[middle+i+1];
            }
            
    while(k<to-from+1){
                
    if(temporary[leftIndex].compareTo(temporary[rightIndex])<0){
                    array[k
    +from]=temporary[leftIndex++];
                }
    else{
                    array[k
    +from]=temporary[rightIndex--];
                }
                k
    ++;
            }
        }
    }
    posted @ 2011-05-27 09:59 思無 閱讀(198) | 評論 (0)編輯 收藏

    一般分如下步驟:
    1)選擇一個樞紐元素(有很對選法,我的實現里采用去中間元素的簡單方法)
    2)使用該樞紐元素分割數組,使得比該元素小的元素在它的左邊,比它大的在右邊。并把樞紐元素放在合適的位置。
    3)根據樞紐元素最后確定的位置,把數組分成三部分,左邊的,右邊的,樞紐元素自己,對左邊的,右邊的分別遞歸調用快速排序算法即可。public static void quicksort(int stru[], int left, int right){

        if(left<right){
            
    int i=left,j=right;
            
    int X=stru[i];
            
    int n=i;
            
    while(i<j){
                
    while(X<stru[j]&&j>i){
                    j
    --;
                }
                stru[n]
    =stru[j];
                n
    =j;
                
    while(X>stru[i]&&i<j){
                    i
    ++;
                }
                stru[n]
    =stru[i];
                n
    =i;
            }
            stru[i]
    =X;
            quicksort(stru,left,j
    -1);
            quicksort(stru,j
    +1,right);
        }
    }
    posted @ 2011-05-26 17:34 思無 閱讀(138) | 評論 (0)編輯 收藏

    1.強引用
    本章前文介紹的引用實際上都是強引用,這是使用最普遍的引用。如果一個對象具有強引用,那就 類似于必不可少的生活用品,垃圾回收器絕不會回收它。當內存空 間不足,Java虛擬機寧愿拋出OutOfMemoryError錯誤,使程序異常終止,也不會靠隨意回收具有強引用的對象來解決內存不足問題。

    2.軟引用(SoftReference)

    如果一個對象只具有軟引用,那就類似于可有可物的生活用品。如果內存空間足夠,垃圾回收器就不會回收它,如果內存空間不足了,就會回收這些對象的內存。只要垃圾回收器沒有回收它,該對象就可以被程序使用。軟引用可用來實現內存敏感的高速緩存。
    軟引用可以和一個引用隊列(ReferenceQueue)聯合使用,如果軟引用所引用的對象被垃圾回收,Java虛擬機就會把這個軟引用加入到與之關聯的引用隊列中。

    3.弱引用(WeakReference)
    如 果一個對象只具有弱引用,那就類似于可有可物的生活用品。弱引用與軟引用的區別在于:只具有弱引用的對象擁有更短暫的生命周期。在垃圾回收器線程掃描它 所管轄的內存區域的過程中,一旦發現了只具有弱引用的對象,不管當前內存空間足夠與否,都會回收它的內存。不過,由于垃圾回收器是一個優先級很低的線程, 因此不一定會很快發現那些只具有弱引用的對象。
    弱引用可以和一個引用隊列(ReferenceQueue)聯合使用,如果弱引用所引用的對象被垃圾回收,Java虛擬機就會把這個弱引用加入到與之關聯的引用隊列中。

    4.虛引用(PhantomReference)
    "虛引用"顧名思義,就是形同虛設,與其他幾種引用都不同,虛引用并不會決定對象的生命周期。如果一個對象僅持有虛引用,那么它就和沒有任何引用一樣,在任何時候都可能被垃圾回收。
    虛 引用主要用來跟蹤對象被垃圾回收的活動。虛引用與軟引用和弱引用的一個區別在于:虛引用必須和引用隊列(ReferenceQueue)聯合使用。當垃 圾回收器準備回收一個對象時,如果發現它還有虛引用,就會在回收對象的內存之前,把這個虛引用加入到與之關聯的引用隊列中。程序可以通過判斷引用隊列中是 否已經加入了虛引用,來了解

    被引用的對象是否將要被垃圾回收。程序如果發現某個虛引用已經被加入到引用隊列,那么就可以在所引用的對象的內存被回收之前采取必要的行動。

    在本書中,"引用"既可以作為動詞,也可以作為名詞,讀者應該根據上下文來區分"引用"的含義。

    在 java.lang.ref包中提供了三個類:SoftReference類、WeakReference類和PhantomReference類,它 們分別代表軟引用、弱引用和虛引用。ReferenceQueue類表示引用隊列,它可以和這三種引用類聯合使用,以便跟蹤Java虛擬機回收所引用的對 象的活動。以下程序創建了一個String對象、ReferenceQueue對象和WeakReference對象:

    //創建一個強引用
    String str = new String("hello");

    //創建引用隊列, <String>為范型標記,表明隊列中存放String對象的引用
    ReferenceQueue<String> rq = new ReferenceQueue<String>();

    //創建一個弱引用,它引用"hello"對象,并且與rq引用隊列關聯
    //<String>為范型標記,表明WeakReference會弱引用String對象
    WeakReference<String> wf = new WeakReference<String>(str, rq);

    以上程序代碼執行完畢,內存中引用與對象的關系如圖11-10所示。

    圖11-10 "hello"對象同時具有強引用和弱引用

    在圖11-10中,帶實線的箭頭表示強引用,帶虛線的箭頭表示弱引用。從圖中可以看出,此時"hello"對象被str強引用,并且被一個WeakReference對象弱引用,因此"hello"對象不會被垃圾回收。
    在以下程序代碼中,把引用"hello"對象的str變量置為null,然后再通過WeakReference弱引用的get()方法獲得"hello"對象的引用:

    String str = new String("hello"); //①
    ReferenceQueue<String> rq = new ReferenceQueue<String>(); //②
    WeakReference<String> wf = new WeakReference<String>(str, rq); //③

    str=null; //④取消"hello"對象的強引用
    String str1=wf.get(); //⑤假如"hello"對象沒有被回收,str1引用"hello"對象

    //假如"hello"對象沒有被回收,rq.poll()返回null
    Reference<? extends String> ref=rq.poll(); //⑥

    執 行完以上第④行后,內存中引用與對象的關系如圖11-11所示,此 時"hello"對象僅僅具有弱引用,因此它有可能被垃圾回收。假如它還沒有被垃圾回收,那么接下來在第⑤行執行wf.get()方法會返 回"hello"對象的引用,并且使得這個對象被str1強引用。再接下來在第⑥行執行rq.poll()方法會返回null,因為此時引用隊列中沒有任 何引用。ReferenceQueue的poll()方法用于返回隊列中的引用,如果沒有則返回null。


    圖11-11 "hello"對象只具有弱引用

    在 以下程序代碼中,執行完第④行后,"hello"對象僅僅具有弱引用。接下來兩次調用System.gc()方法,催促垃圾回收器工作,從而提 高"hello"對象被回收的可能性。假如"hello"對象被回收,那么WeakReference對象的引用被加入到ReferenceQueue 中,接下來wf.get()方法返回null,并且rq.poll()方法返回WeakReference對象的引用。圖11-12顯示了執行完第⑧行后 內存中引用與對象的關系。

    String str = new String("hello"); //①
    ReferenceQueue<String> rq = new ReferenceQueue<String>(); //②
    WeakReference<String> wf = new WeakReference<String>(str, rq); //③
    str=null; //④

    //兩次催促垃圾回收器工作,提高"hello"對象被回收的可能性
    System.gc(); //⑤
    System.gc(); //⑥
    String str1=wf.get(); //⑦ 假如"hello"對象被回收,str1為null
    Reference<? extends String> ref=rq.poll(); //⑧


    圖11-12 "hello"對象被垃圾回收,弱引用被加入到引用隊列

    posted @ 2011-05-26 16:33 思無| 編輯 收藏

    僅列出標題
    共3頁: 上一頁 1 2 3 
    主站蜘蛛池模板: 成人免费视频试看120秒| 亚洲日韩人妻第一页| 亚洲av无码专区在线电影| 全部免费毛片免费播放| 一级做a爰片久久免费| 亚洲视频在线观看免费| 成人在线免费观看| 91视频精品全国免费观看| 亚洲国产精品美女| 免费v片在线观看无遮挡| 久久免费观看国产精品| 亚洲精品无码国产片| 亚洲va久久久噜噜噜久久天堂| 最近中文字幕免费mv视频7| 中文在线观看国语高清免费| 亚洲国产精品综合一区在线| 亚洲天堂中文字幕在线| 国产成人免费网站| 三上悠亚电影全集免费| 亚洲精品天堂成人片AV在线播放| 日本红怡院亚洲红怡院最新| 在线免费观看色片| 免费看搞黄视频网站| 美女被免费网站在线视频免费| 久久久无码精品亚洲日韩京东传媒| 免费乱理伦在线播放| 久久久久久精品免费免费自慰| caoporn国产精品免费| 亚洲另类春色校园小说| 亚洲精品无码久久久久去q| 在线免费一区二区| 114级毛片免费观看| 亚美影视免费在线观看| 亚洲国产精品网站在线播放 | 国产精品区免费视频| 国产午夜亚洲精品不卡| 7777久久亚洲中文字幕| 中文字幕亚洲精品资源网| 久久精品国产亚洲网站| 亚洲日韩在线观看| 国产免费人视频在线观看免费|