me: (immediately) Yes!
Colleague: Why would one do that?
me: To disallow other classes to instantiate that class
Colleague: But is that class good for anything if other classes cannot instantiate it?
me: Yeah, even if it is a private constructor you can instantiate that class within that class (i.e. inside its own method(s)) and other classes would use that method to access the instance of that class.
Colleague: But, then I don't see any use of making it private then.
me: When you make things private from rest of the world, you basically want to restrict/control the access of those private things. That is what you can achieve it here too. In this case, you want to control instantiation of the class.
me: For example, how can you make sure that class can be instantiated only once?
Colleague: Ummmm... (thinking for a while). I can declare a variable, let say count inside a class and will track object instances using that. So if count is 1, I won't allow any new object instantiation of that class.
me: But, that count variable (assuming he is referring to object variable) will be zero (or whatever set to default) for every new instance of that class so, how that will help you?
Colleague: No, no. It will be a static variable (class variable) and all object instances will share that.
me: Ya, you can track object instances using static variable. But for sharing that single instance of class at other places in your application, you either need to declare it globally or declare it as a static variable inside that class only. The later is good, because once you do that you probably don't need that static count variable. Ya, you don't need. Because you can always check if that instance is not null and that means it is already instantiated. So, return that instance only. Man, what we are discussing is nothing but a singleton pattern only.
So in Java, it will be like,
public class MyClass {
private static MyClass myClassUniqueInstance;
private MyClass() { }
public static synchronized MyClass getInstance() {
if (myClassUniqueInstance == null) {
myClassUniqueInstance = new MyClass();
}
return myClassUniqueInstance;
}
}
The synchronized keyword is added in getInstance() method to make it thread safe.
Okay, so trouble for posting this conversation on blog: To let you,
1. ask these kind of questions to yourself and/or others (may be good for interviewer)
2. answer these kind of questions (good for interviewee)
3. know at least one design pattern i.e. "The singleton pattern".

42 comments:
美女貼圖偷拍貼圖台中寫真棉花田本土美女寫真影片免費觀賞無碼光碟免費交友go交友覓戀會館正妹交友goking7777net免費交友ggo台灣18成人新竹聊天室網愛聊天室寫真集影片免費寫真女郎影片情色典獄長自拍照918色情故事
以簡單的行為愉悅他人的心靈,勝過千人低頭禱告........................................
淫女情慾情色做愛限制級波霸口交18禁貼圖寫真視訊援交露點爆乳潮吹裸體裸照裸女愛愛無碼尋夢視訊聊天a漫a片a圖一夜情一葉情人妻激情情色寫真美女自拍辣妹自拍正妹自拍美女走光
very popular to u! ........................................
這麼好的部落格,以後看不到怎麼辦啊!!!..................................................
加油啦!要繼續發表好文章喔! ..................................................
很棒的分享~留言支持!........................................
thank you for you to make me learn more,thank you∩0∩ ........................................
文章優,圖片美,就連回文都很有意思~~~ ........................................
85cc片觀看,77美女dvd影片,熊貓貼區,ut網際聊天,一葉情,av,嘟嘟,影音live秀,a片,做愛影片,視訊做愛,美女短片,78論壇,ut聊天,打飛機,a片,免費視訊,免費視訊,成人影院,性愛小說,辣妹視訊,網路交友,捷克論壇,h影片,色咪咪,免費影片85cc,kiss911,後宮,a片,影音視訊聊天,交友,免費聊天,聊天室交友,做愛影片,線上a片,美女影片,免費影片下載,免費聊天室,視訊做愛,美女視訊聊天
wonderful...................................................
這麼好的部落格,以後看不到怎麼辦啊!!!..................................................
人因夢想而偉大,要堅持自己的理想哦!..................................................
這麼好的文章當然要回應支持(>▽<)........................................
上來打聲招呼,祝你一切平安! ........................................
45avdvd 本土av自拍天堂 免費a長片 性感照片xxx383 av1688情人天使娛樂網,sex 台中援交34c甜心寶貝直播貼片 hhh重鹹系列免費 寶貝視訊台中援交友留言 桃園視訊聊天室 小魔女a自拍 上班族視訊 高雄援留言視訊交友 彩虹性愛巴士 夢中情人情趣用品 mmshow tw網愛 苗栗人聊天室 寶貝視訊ok論壇 0941電愛專線 520sex-情色視訊 免費影片無碼卡通 85cc情色視訊交友 免費視訊美女影音觀賞 色瞇瞇影片網 jp成人 免費a影片線上直播 h 視訊自拍美女聊天室 性感沙灘3,性感照片 視訊交友90739,限制級,777美女dvd 洪爺sex免費看a片論壇 微風成人區情色文學 微風論壇短片區 免費成人動畫 玩美辣妹有約 1111av女優天堂 嘟嘟成人網 視訊聊天1799 成人圖片區 台灣kiss,微風成人 show live 視訊聊天 g8成人下載 yam視訊交友 a 免費影片線上直播 情色視訊交友辣妹no3 日本a片援交妹 拓網天空交友高雄視訊 6k情人網辣妹視訊 正妹視訊elove交友 免費視訊formosa car 視訊聊天qk176 成人文學
在你一無所有的時候 是誰在陪伴你 他便是你最重要的人 ............................................................
健康的身體是事業成功的基礎。 ............................................................
累死了…來去看看文章轉換心情~ .........................................
文章不求沽名釣譽,率性就是真的..................................................................
要持續更新下去喲!!祝你心情愉快.............................................................
在莫非定律中有項笨蛋定律:「一個組織中的笨蛋,恆大於等於三分之二。」.................................................................
成熟,就是有能力適應生活中的模糊。.................................................................
成熟,就是有能力適應生活中的模糊。.................................................................
噴泉的高度,不會超過它的源頭。一個人的事業也是如此,它的成就絕不會超過自己的信念。.................................................................
海鷗要高飛,必先遠退。花蜜要香醇,必先久釀。............................................................
唯有用熱情、用智慧去觀察事物,這事物才會把他的秘密,洩漏給我們............................................................
下次再來希望可以看到新的作品喔。............................................................
噴泉的高度,不會超過它的源頭。一個人的事業也是如此,它的成就絕不會超過自己的信念。..................................................................
Riches serve a wise man but command a fool.............................................................
好想睡覺哦~上班摸魚看看blog..................................................................
Always keep your words soft and sweet, just in case you have to eat them...................................................
這麼用心的經營你的文章, 當然值得我們留連拜訪的!...............................................................
安安唷~~幸運的日子送給妳(你)滿滿的幸福,也祝福你天天都開心唷..................................................................
It is easier to get than to keep it.......................................................................
你不能決定生命的長度,但你可以控制它的寬度..................................................................
I have nothing to offer but blood, toil, tears and sweat...................................................................
原來這世上能跟你共同領略一個笑話的人竟如此難得......................................................................
與人相處不妨多用眼睛說話,多用嘴巴思考. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
不要去想沒拿到的東西,多想想自己手裡所擁有的..................................................
看到大家都留言-我也忍不住說聲---加油..................................................
Post a Comment