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

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.

主站蜘蛛池模板: 新闻| 凤庆县| 桃园县| 阿合奇县| 花莲市| 巨鹿县| 巴楚县| 苍南县| 临汾市| 贵南县| 河池市| 湘潭市| 阿坝县| 灌南县| 吉首市| 宁晋县| 韶山市| 淳化县| 鹤壁市| 长白| 余姚市| 加查县| 望都县| 凯里市| 清徐县| 玉环县| 汝州市| 临高县| 剑河县| 治县。| 保山市| 阿拉尔市| 康保县| 辛集市| 镶黄旗| 乌兰浩特市| 常宁市| 泰州市| 阿拉善左旗| 鄂托克前旗| 阿拉善盟|