首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

单例模式-<ant谋职记之设计模式>

2012-11-09 
单例模式-----ant求职记之设计模式??? ?大家好,我是ant,今天是我开始创作《ant求职记之设计模式》的第一天

单例模式-----<ant求职记之设计模式>

??? ?大家好,我是ant,今天是我开始创作《ant求职记之设计模式》的第一天,作为一名刚踏出象牙塔的应届毕业生,跟很多人一样,在经历着人生的一个特殊历程:求职。这对我们每个人说都是十分重要的。而求职的艰辛或许大家都能体会得到。

????? 最近翻看了表哥给我的《Head First Design Pattern》,感觉很有意思,于是产生了一个想法,将自己的求职经历用各种设计模式表现出来。这不仅仅是对design pattern的学习总结,也可以算是对求职历程的程序记录。相信,它会变得很有意思的。

????? 恩,好了,现在俺切入正题,开始我们的设计模式之旅吧。单例模式-<ant谋职记之设计模式>

???

???? ?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 instantcepublic 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 resumeclass Resume implements Runnable {@Overridepublic void run() {StuAnt ant = StuAnt.getInstance();ant.getPersonInfo();}}// check the resums whether describe me in the correct way or notpublic 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 jxxysorry,it's not a single pattern

?

my name is geyubin and my graduteSchool is jxxymy name is geyubin and my graduteSchool is jxxy

?

sorry,it's not a single patternmy name is geyubin and my graduteSchool is jxxy

?

??

sorry,it's not a single patternsorry,it's not a single pattern

?

?

?? 单例模式-<ant谋职记之设计模式>怎么程序简历里面描述的对象不一样的,纳闷,怎不是单例。

??? 难以想象在世界的某个角落居然还有另一个“自己”。22岁的偶,做梦也没遇见过“太空人”着,咋,什么时候就被“克隆”了呢。

??? 简历里面描述的是另一个自己,那可不是开玩笑的。为了寻求答案,我翻出了自己的“葵花宝典”《head first? design pattern》,琢磨一阵,终于理出了思路:

?

antResume1:

?

1.public static StuAnt getInstance() {
2.?if (ant == null) {
3.??????ant = new StuAnt();?
4.??stuNum++;
5.??}
6.??return ant;
7.}

antResume2:

?

1.public static StuAnt getInstance() {

2.
3.?if (ant == null) {
4.?

5.??ant = new StuAnt();?
6.???stuNum++;
7.??}
8.??return ant;
9.?}

?

?????

??? 大家看看上面的两个线程吧,这只是其中的一个特例,最后导致了:

?

???????

my name is geyubin and my graduteSchool is jxxysorry,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 instantcepublic 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 jxxymy name is geyubin and my graduteSchool is jxxy

?

??? 单例模式-<ant谋职记之设计模式>,永远都是同一个实例,(*^__^*) ----简历里的俺是唯一的,不可替代的。?ok,这应该是一个完整的单例模式了。

???? 准备既然准备好了简历,过几天就去人才市场看看,欲知后事如何,请看“下节”------抽象工厂模式

?

?

??? 谢谢大家的观看

???

???

?

????

?

?

?

????

?

?

?

?

?

?

?

?

?

???

热点排行