??? ?大家好,我是ant,今天是我開始創作《ant求職記之設計模式》的第一天,作為一名剛踏出象牙塔的應屆畢業生,跟很多人一樣,在經歷著人生的一個特殊歷程:求職。這對我們每個人說都是十分重要的。而求職的艱辛或許大家都能體會得到。
????? 最近翻看了表哥給我的《Head First Design Pattern》,感覺很有意思,于是產生了一個想法,將自己的求職經歷用各種設計模式表現出來。這不僅僅是對design pattern的學習總結,也可以算是對求職歷程的程序記錄。相信,它會變得很有意思的。
????? 恩,好了,現在俺切入正題,開始我們的設計模式之旅吧。
???
???? ?ok,首先簡單介紹下我自己吧,請看如下代碼:
package com.ant.single.domain; public class StuAnt { private static StuAnt ant; private static int stuNum=0; private String name = "geyubin"; private String college = "jxxy"; private StuAnt() { } //get the single instantce public static StuAnt getInstance() { if (ant == null) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } ant = new StuAnt(); stuNum++; } return ant; } public void getPersonInfo() { if(stuNum==1){ System.out.println("my name is " + name + " and my graduteSchool is " + college); }else{ System.out.println("sorry,it's not a single pattern"); } } }
?
?? ? 在本人信息類里面,我將構造方法私有化了,這樣保證了其它類無法直接創建StuAnt的實例,而getInstance()作為訪問StuAnt類的唯一入口,提供了單一StuAnt實例的創建。?由于本人是唯一的,為了檢驗這種特性,我在getPersonInfo()中調用了stuNum。
??????為了描述自己,我寫了兩份不同的簡歷,并進行檢查是否描述的是同一個我:
package com.ant.single.test; import com.ant.single.domain.StuAnt; //the class of ant resume class Resume implements Runnable { @Override public void run() { StuAnt ant = StuAnt.getInstance(); ant.getPersonInfo(); } } // check the resums whether describe me in the correct way or not public class StuTest { public static void main(String[] args) { Resume antResume = new Resume(); Thread antResume1 = new Thread(antResume); Thread antResume2 = new Thread(antResume); antResume1.start(); antResume1.start(); } }
?
跑一下,我猜兩份簡歷描述的應該是同一個對象吧:
my name is geyubin and my graduteSchool is jxxy sorry,it's not a single pattern
?
my name is geyubin and my graduteSchool is jxxy my name is geyubin and my graduteSchool is jxxy
?
sorry,it's not a single pattern my name is geyubin and my graduteSchool is jxxy
?
??
sorry,it's not a single pattern sorry,it's not a single pattern
?
?
??
怎么程序簡歷里面描述的對象不一樣的,納悶,怎不是單例。
??? 難以想象在世界的某個角落居然還有另一個“自己”。22歲的偶,做夢也沒遇見過“太空人”著,咋,什么時候就被“克隆”了呢。
??? 簡歷里面描述的是另一個自己,那可不是開玩笑的。為了尋求答案,我翻出了自己的“葵花寶典”《head first? design pattern》,琢磨一陣,終于理出了思路:
?
antResume1: ?
|
antResume2: ?
? |
?????
??? 大家看看上面的兩個線程吧,這只是其中的一個特例,最后導致了:
?
???????
my name is geyubin and my graduteSchool is jxxy sorry,it's not a single pattern
?
??? 知道了問題點,那我們該想下如何讓線程同步呢,O(∩_∩)O~,sychronized,恩。請看修改后的StuAnt代碼:
package com.ant.single.domain; public class StuAnt { private static StuAnt ant; private static Integer stuNum=0; private String name = "geyubin"; private String college = "jxxy"; private StuAnt() { } //get the single instantce public static StuAnt getInstance() { if (ant == null) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (stuNum) { if(ant==null){ ant = new StuAnt(); stuNum++; } } } return ant; } public void getPersonInfo() { if(stuNum==1){ System.out.println("my name is " + name + " and my graduteSchool is " + college); }else{ System.out.println("sorry,it's not a single pattern"); } } }
?
??? 好了,現在再跑跑程序:
my name is geyubin and my graduteSchool is jxxy my name is geyubin and my graduteSchool is jxxy
?
???
,永遠都是同一個實例,(*^__^*) ----簡歷里的俺是唯一的,不可替代的。?ok,這應該是一個完整的單例模式了。
???? 準備既然準備好了簡歷,過幾天就去人才市場看看,欲知后事如何,請看“下節”------抽象工廠模式
?
?
??? 謝謝大家的觀看
???
???
?
????
?
?
?
????
?
?
?
?
?
?
?
?
?
???
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
