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

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.

主站蜘蛛池模板: 邛崃市| 灵石县| 永平县| 随州市| 孟州市| 六安市| 称多县| 叙永县| 香河县| 石城县| 石棉县| 唐海县| 班戈县| 建昌县| 丽江市| 明光市| 志丹县| 郎溪县| 上杭县| 财经| 天镇县| 尚义县| 五家渠市| 玛沁县| 伊通| 平谷区| 田林县| 阿巴嘎旗| 任丘市| 开远市| 清丰县| 运城市| 松江区| 桂东县| 德钦县| 铜川市| 介休市| 吉林省| 霍州市| 玉屏| 漳浦县|