dongma1666 2018-07-24 19:49
浏览 80

维护:Singleton vs Dependency Injection

I have a script comprising lots of classes. In the classes' functions numerous mysql operations are carried out. I want to use a single datebase handle throughout the script. Thus, the Singleton approach or dependency injection (DI) might do this job.

However, I am wondering if I am right that using Singleton is the more efficient way from a maintainance point of view:

Assuming that all my (parent) classes finally extend the singleton class. Thus, in order to change something with respect to obtaining the database handle, I only need to do amendments to the singleton class. Particularly, I do not have to make any thoughts when adding new classes or the like since the database handle will be always present by extending the respective class of the Singleton class.

In contrast, when using DI I must provide code for creating the database handle in every script which might serve as a starting point (and passing the handle to the instances created). E.g. in script1.php, script2.php, script3.php, etc. Thus, amendments which should be made to the way the database handle is obtained must be made in every script, script1.php, script2.php, script3.php, etc..

The former approach looks much nicer to me in this respect than the latter. Or do I oversee some important thing?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制