`
- 浏览:
327331 次
- 来自:
上海交通大学软件学院
-
Head First Design Pattern - 5 - This week's interview: Confe
HeadFirst: Today we are pleased to bring you an interview with a
Singleton object. Why don’t you begin by telling us a bit about
yourself?
Singleton: Well, I’m totally unique; there is just one of me!
HeadFirst: One?
Singleton: Yes, one. I’m based on the Singleton Pattern, which assures that at any one time there is only one instance of me.
HeadFirst: Isn’t that sort of a waste? Someone took the time to develop
a full-blown class and now all we can get is one object out of it?
Singleton: Not at all! There is power in ONE. Let’s say you have an
object that contains registry settings. You don’t want multiple copies
of that object and its values running around – that would lead to
chaos. By using an object like me you can assure that every object in
your application is making use of the same global resource.
HeadFirst: Tell us more…
Singleton: Oh, I’m good for all kinds of things. Being single sometimes
has its advantages you know. I’m often used to manage pools of
resources, like connection or thread pools.
HeadFirst: Still, only one of your kind? That sounds lonely.
Singleton: Because there’s only one of me, I do keep busy, but it would
be nice if more developers knew me – many developers run into bugs
because they have multiple copies of objects floating around they’re
not even aware of.
HeadFirst: So, if we may ask, how do you know there is only one of you? Can’t anyone with a new operator create a “new you”?
Singleton: Nope! I’m truly unique.
HeadFirst: Well, do developers swear an oath not to instantiate you more than once?
Singleton: That’s right. My constructor is declared private.
HeadFirst: How does that work? How do you EVER get instantiated?
Singleton: You see, to get a hold of a Singleton object, you don’t
instantiate one, you just ask for an instance. So my class has a static
method called getInstance(). Call that, and I’ll show up at once, ready
to work. In fact, I may already be helping other objects when you
request me.
HeadFirst: Well, Mr. Singleton, there seems to be a lot under your
covers to make all this work. Thanks for revealing yourself and we hope
to speak with you again soon!
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
iOS快速样板 应用程序在iOS应用程序中以快速方式实现架构,并在MVVM中使用Swinject和RxSwift。 入门 在样例文件中添加Rubygen.rb,在新对象上创建永久性副本,在每个样例中都添加了示例... -f Forza gli input di confe
ApiRTC 该存储库包含用于将ApiRTC集成到Web应用程序中的实时通信的示例。 ApiRTC是WebRTC PaaS(平台即服务),可简化开发人员对WebRTC技术的访问。 使用我们的API,只需几行代码即可向您的网站和移动应用... Confe
Conference Calendar CONFERENCE CALENDAR Conference Calendar International Gene Therapy Meetings 2001 ...Gene Vector Production Network Conference October 4–5, Genocentre, Evry, France... Siebens Confe
Conference Calendar CONFERENCE CALENDAR Conference Calendar International Gene Therapy Meetings 2001 ...Gene Vector Production Network Conference October 4–5, Genocentre, Evry, France... Siebens Confe
CVPR2020:如何训练您的深度多对象跟踪器 新闻... Yihong and Osep, Aljosa and Ban, Yutong and Horaud, Radu and Leal-Taix{\'e}, Laura and Alameda-Pineda, Xavier}, booktitle={Proceedings of the IEEE/CVF Confe
尖峰网络的基于事件的角速度回归 这是Mathias Gehrig,Sumit Bam Shrestha,Daniel Mouritzen和的论文《基于事件的带有尖峰网络的角速度回归》的代码。... journal = { {IEEE} International Confe
5. 现在输入 `:so $MYVIMRC` 或 `:source ~/.vimrc`,这将从当前的 `.vimrc` 文件加载新的设置,无需重启 Vim。 为了让这个过程更便捷,你可以为编辑和重载 `.vimrc` 创建快捷键映射。在你的 `.vimrc` 文件中添加...
诊断时间动作检测器(DETAD)中的错误该存储库旨在承载用于分析时间动作本地化算法的诊断工具。 它包括三个分析:假阳性分析,定位度量敏感性分析和假... Victor and Ghanem, Bernard}, booktitle={The European Confe
pyGeo pyGeo是面向对象的几何处理框架,用于多学科设计优化。 它提供了基于自由变形(FFD)的几何处理对象,与NASA的Vehicle Sketch Pad几何引擎的接口,简单的几何约束公式化对象以及一些用于几何处理的实用... @confe