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

  • Unity Game Optimization
  • Dr. Davide Aversa Chris Dickinson
  • 217字
  • 2021-06-24 12:13:05

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.

主站蜘蛛池模板: 阿图什市| 邹平县| 峨山| 金坛市| 革吉县| 抚松县| 周宁县| 南平市| 徐汇区| 金秀| 文山县| 溧水县| 宝鸡市| 鄄城县| 贵定县| 凤城市| 汽车| 定兴县| 灌云县| 稻城县| 宁阳县| 泽普县| 南投市| 华阴市| 阿拉善盟| 武清区| 女性| 新建县| 黄山市| 左云县| 禄劝| 安龙县| 中方县| SHOW| 开化县| 富源县| 交口县| 高要市| 洪江市| 建平县| 鹰潭市|