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

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 as to whether 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 a difficult task to refactor a singleton out of our code base. 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.

主站蜘蛛池模板: 澄城县| 永善县| 宝兴县| 垫江县| 尼勒克县| 达州市| 洛川县| 嘉义市| 鄂尔多斯市| 台南县| 浦东新区| 河南省| 洛宁县| 泰安市| 吉首市| 平原县| 集贤县| 奈曼旗| 嵩明县| 南康市| 循化| 焉耆| 云阳县| 安仁县| 全南县| 游戏| 巩义市| 义乌市| 通化市| 山丹县| 郁南县| 睢宁县| 双牌县| 拉萨市| 奉贤区| 丁青县| 克拉玛依市| 祥云县| 广德县| 定结县| 罗城|