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

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.

主站蜘蛛池模板: 潢川县| 南木林县| 峨眉山市| 十堰市| 新野县| 滕州市| 浦东新区| 盘锦市| 广宗县| 台江县| 新龙县| 德安县| 化隆| 万盛区| 正镶白旗| 富蕴县| 凯里市| 泽州县| 北碚区| 茂名市| 山东| 乐山市| 永康市| 加查县| 四平市| 宁明县| 临泽县| 临海市| 沛县| 玉山县| 莒南县| 宁远县| 化州市| 六枝特区| 清远市| 苗栗市| 晋州市| 依安县| 黄浦区| 金秀| 大关县|