2011年11月25日 星期五

Singleton

http://my.so-net.net.tw/idealist/Patterns/Singleton.html



Singleton
(史帝芬, idealist@gcn.net.tw, 2003/10/21)
當系統中某項資源只有一個,而且絕對獨一無二時,最適合使用這個Pattern,也就是說使用這個Pattern可以確保 物件個體只有一個,不會因programmer的疏忽而產生兩個或兩個以上。
  同一個Pattern,用不同的語言實作會因語言本身的特性與限制而有些許的不同,這裡會展現Java和C++兩種版本。
  • Java版

  • Singleton.java
    public class Singleton {
     private static Singleton singleton = new Singleton();
     private Singleton() {
      System.out.println("已產生物件個體");
     }

     public static Singleton getInstance() {
      return singleton;
     }
    }

    說明: 注意看上面的建構式,是private ! 也就是說用 new Singleton也出現編譯錯誤,如此就可以防止人為疏失,那麼這 個class要怎麼使用呢? 方法如下…
    public class Main {
     public static void main(String[] args) {
      Singleton obj1 = Singleton.getInstance();
      //do something…
     }
    }

  • C++版

  • Singleton.h
    class Singleton {
    private:
     static Singleton* singleton;
     int count;
     Singleton();
    public:
     static Singleton* getInstance();
     void setCount(int c);
     int getCount();
    };

    Singleton.cpp
    #include "Singleton.h"

    Singleton* Singleton::singleton = 0;

    Singleton::Singleton() { }

    Singleton* Singleton::getInstance()
    {
     if (singleton == 0) {
      singleton = new Singleton;
     }
     return singleton;
    }

    void Singleton::setCount(int c)
    {
     count = c;
    }

    int Singleton::getCount()
    {
     return count;
    }

    main.cpp (使用範例)
    #include "Singleton.h"
    #include "stdio.h"

    int main() {
     Singleton* singleton1 = Singleton::getInstance();
     Singleton* singleton2 = Singleton::getInstance();
     singleton1->setCount(100);
     printf("singleton2->count: %d\n", singleton2->getCount());
     singleton2->setCount(200);
     printf("singleton1->count: %d\n", singleton1->getCount());

     return 1;
    }

    執行結果
    singleton2->count: 100
    singleton1->count: 200

    說明:
    有人或許會想,寫個普通的class,然後設一全域變數不就行了? 這個想法有缺點! 因為你沒辦法 預防其它人又new了一個物件。

區域連線快速斷線方法?

http://social.technet.microsoft.com/Forums/zh-TW/windowsvistaclientzhcht/thread/2668d5ed-3bc1-431c-94a9-b1400560cdc2/





已答覆 區域連線快速斷線方法? 

  • 2009年3月24日 上午 08:14
     
    我的電腦是用windows xp pro sp2的
    現在我造了一個捷徑, 當區域連線停用了的時候,只要按一下就可以自動連線
    但是,要斷線時,卻要按一下右下角的"區域連線"-->停用 才可以。
    想問一問有沒有方法可以像"捷徑"一般,按一下就斷線呢?

    如果捷徑做不到,不知道dos command 有沒有方法可以做到呢?(生成xxx.bat)

    謝謝解答問題!

解答

  • 2009年3月24日 下午 12:08
    IronMouse 的 Avatar
    (MVP)
    7,275
     已答覆包含代碼
    HI:
    大致上有3種方法
    1.
    使用devcon工具去停用或啟用網路卡,請參考
    http://alexchuo.blogspot.com/2004/10/blog-post_13.html
    2.
    如果你是Windows 2003,可以用下列指令
    停用
    netsh interface set interface "區域連線" disabled
    啟用
    netsh interface set interface "區域連線" enabled
    區域連線這個名稱改成你要停用或啟用的連線名稱,可以在控制台中的網路連線找到
    XP的話,要從Windows 2003拿ifmon.dll覆蓋XP的檔案
    3.
    用以下vb script,把以下內容存成XXX.vbs,點2下執行,會把啟用狀態改為停用,或者停用改為啟用
    其中幾個參數可能要根據電腦作修改
    sConnectionName = "區域連線"
    區域連線這個名稱改成你要停用或啟用的連線名稱,可以在控制台中的網路連線找到
    Const ssfCONTROLS = 3   
     
    sConnectionName = "區域連線"   
     
    sEnableVerb = "啟用(&A)"   
    sDisableVerb = "停用(&B)"   
     
    set shellApp = createobject("shell.application")   
    set oControlPanel = shellApp.Namespace(ssfCONTROLS)   
     
    set oNetConnections = nothing   
    for each folderitem in oControlPanel.items   
    if folderitem.name = "網路連線" then   
    set oNetConnections = folderitem.getfolder: exit for   
    end if   
    next   
     
    if oNetConnections is nothing then   
    msgbox "Couldn't find '網路連線' folder"   
    wscript.quit   
    end if   
     
    set oLanConnection = nothing   
    for each folderitem in oNetConnections.items   
    if lcase(folderitem.name) = lcase(sConnectionName) then   
    set oLanConnection = folderitem: exit for   
    end if   
    next   
     
    if oLanConnection is nothing then   
    msgbox "Couldn't find '" & sConnectionName & "' item"   
    wscript.quit   
    end if   
     
    bEnabled = true   
    set oEnableVerb = nothing   
    set oDisableVerb = nothing   
    s = "Verbs: " & vbcrlf   
    for each verb in oLanConnection.verbs   
    ss = s & vbcrlf & verb.name   
    if verb.name = sEnableVerb then   
    set oEnableVerb = verb   
    bEnabled = false   
    end if   
    if verb.name = sDisableVerb then   
    set oDisableVerb = verb   
    end if   
    next   
     
    'debugging displays left just in case...   
    '   
    'msgbox s ': wscript.quit   
    'msgbox "Enabled: " & bEnabled ': wscript.quit   
     
    'not sure why, but invokeverb always seemed to work   
    'for enable but not disable.   
    '   
    'saving a reference to the appropriate verb object   
    'and calling the DoIt method always seems to work.   
    '   
    if bEnabled then   
    ' oLanConnection.invokeverb sDisableVerb   
    oDisableVerb.DoIt   
    else   
    ' oLanConnection.invokeverb sEnableVerb   
    oEnableVerb.DoIt   
    end if   
     
    'adjust the sleep duration below as needed...   
    '   
    'if you let the oLanConnection go out of scope   
    'and be destroyed too soon, the action of the verb   
    'may not take...   
    '   
    wscript.sleep 1000 
    謝謝
    • 已標示為解答kcshining2009年3月25日 上午 02:49
    •  

所有回覆

  • 2009年3月24日 下午 12:08
    IronMouse 的 Avatar
    (MVP)
    7,275
     已答覆包含代碼
    HI:
    大致上有3種方法
    1.
    使用devcon工具去停用或啟用網路卡,請參考
    http://alexchuo.blogspot.com/2004/10/blog-post_13.html
    2.
    如果你是Windows 2003,可以用下列指令
    停用
    netsh interface set interface "區域連線" disabled
    啟用
    netsh interface set interface "區域連線" enabled
    區域連線這個名稱改成你要停用或啟用的連線名稱,可以在控制台中的網路連線找到
    XP的話,要從Windows 2003拿ifmon.dll覆蓋XP的檔案
    3.
    用以下vb script,把以下內容存成XXX.vbs,點2下執行,會把啟用狀態改為停用,或者停用改為啟用
    其中幾個參數可能要根據電腦作修改
    sConnectionName = "區域連線"
    區域連線這個名稱改成你要停用或啟用的連線名稱,可以在控制台中的網路連線找到
    Const ssfCONTROLS = 3   
     
    sConnectionName = "區域連線"   
     
    sEnableVerb = "啟用(&A)"   
    sDisableVerb = "停用(&B)"   
     
    set shellApp = createobject("shell.application")   
    set oControlPanel = shellApp.Namespace(ssfCONTROLS)   
     
    set oNetConnections = nothing   
    for each folderitem in oControlPanel.items   
    if folderitem.name = "網路連線" then   
    set oNetConnections = folderitem.getfolder: exit for   
    end if   
    next   
     
    if oNetConnections is nothing then   
    msgbox "Couldn't find '網路連線' folder"   
    wscript.quit   
    end if   
     
    set oLanConnection = nothing   
    for each folderitem in oNetConnections.items   
    if lcase(folderitem.name) = lcase(sConnectionName) then   
    set oLanConnection = folderitem: exit for   
    end if   
    next   
     
    if oLanConnection is nothing then   
    msgbox "Couldn't find '" & sConnectionName & "' item"   
    wscript.quit   
    end if   
     
    bEnabled = true   
    set oEnableVerb = nothing   
    set oDisableVerb = nothing   
    s = "Verbs: " & vbcrlf   
    for each verb in oLanConnection.verbs   
    ss = s & vbcrlf & verb.name   
    if verb.name = sEnableVerb then   
    set oEnableVerb = verb   
    bEnabled = false   
    end if   
    if verb.name = sDisableVerb then   
    set oDisableVerb = verb   
    end if   
    next   
     
    'debugging displays left just in case...   
    '   
    'msgbox s ': wscript.quit   
    'msgbox "Enabled: " & bEnabled ': wscript.quit   
     
    'not sure why, but invokeverb always seemed to work   
    'for enable but not disable.   
    '   
    'saving a reference to the appropriate verb object   
    'and calling the DoIt method always seems to work.   
    '   
    if bEnabled then   
    ' oLanConnection.invokeverb sDisableVerb   
    oDisableVerb.DoIt   
    else   
    ' oLanConnection.invokeverb sEnableVerb   
    oEnableVerb.DoIt   
    end if   
     
    'adjust the sleep duration below as needed...   
    '   
    'if you let the oLanConnection go out of scope   
    'and be destroyed too soon, the action of the verb   
    'may not take...   
    '   
    wscript.sleep 1000 
    謝謝
    • 已標示為解答kcshining2009年3月25日 上午 02:49
    •  
  • 2009年3月25日 上午 02:50
     
    那段vb script十分有效,
    謝謝你的解答!
需要協助使用論壇嗎?(常見問題集)

統計資料

  • 開始時間: 2009/3/24
  • 最後回覆: 2009/3/25
  • 有用的投票: 0
  • 回覆: 2
  • 檢視: 5,131