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

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.

主站蜘蛛池模板: 宾川县| 织金县| 乌什县| 河源市| 宜州市| 长葛市| 沧源| 运城市| 鲁甸县| 奉贤区| 兰坪| 田东县| 松原市| 军事| 清新县| 达孜县| 陈巴尔虎旗| 庐江县| 晴隆县| 石景山区| 岑溪市| 汉沽区| 康平县| 鸡东县| 湘潭县| 宾阳县| 县级市| 双城市| 安仁县| 城口县| 长葛市| 肇州县| 永兴县| 庐江县| 赤城县| 林周县| 柳州市| 方山县| 雅安市| 南召县| 崇阳县|