官术网_书友最值得收藏!

A globally accessible object

The first requirement makes the Messaging System an excellent candidate for a Singleton object, since we would only ever need one instance of the system. Although, it is wise to think long and hard if this is truly the case before committing to implementing a Singleton.

If we later decide that we want multiple instances of this object to exist, wish to allow the systems to be created/destroyed during runtime, or even wish to create test cases that allow us to fake or create/destroy them in the middle of a test, then it can be difficult task to refactor a Singleton out of our codebase. This is due to all of the dependencies we will gradually introduce to our code as we use the system more and more.

If we wish to avoid Singletons due to the above drawbacks, then it may be easier to create a single instance of the Messaging System during initialization and then pass it around from subsystem to subsystem as needed, or we might wish to go further and explore the concept of Dependency Injection, which attempts to solve problems like these. However, for the sake of simplicity, we will assume that a Singleton fits our needs and design our MessagingSystem class accordingly.

主站蜘蛛池模板: 磐安县| 亚东县| 伊川县| 鞍山市| 武威市| 喀什市| 泰来县| 迭部县| 陵水| 高密市| 澳门| 林口县| 承德市| 太白县| 安塞县| 长武县| 吴忠市| 珠海市| 辽宁省| 包头市| 竹山县| 通许县| 石阡县| 彰化市| 五莲县| 康乐县| 龙江县| 遂溪县| 水富县| 荥阳市| 泾川县| 汝州市| 宣威市| 大厂| 齐齐哈尔市| 邯郸市| 葵青区| 达拉特旗| 汨罗市| 云梦县| 木里|