博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Layers, Levels, and DIP铪铪
阅读量:4191 次
发布时间:2019-05-26

本文共 4387 字,大约阅读时间需要 14 分钟。

Uncle Bob's Software Craftsmanship Corner
Layers, Levels, and DIP
by Robert C. Martin
February 20, 2004
Summary
It is often said that high level layers should depend on lower level layers. However the Dependency Inversion Principle says that high level policy should not depend upon low level details. What's up with that?

As I read through DDD (Domain Driven Design by Eric Evans), I am concerned by the concept of Layers. I know this is an old saw, but I'm beginning to wonder about it.

DDD describes the following layers in this order:

UI--->App--->Domain--->Infrastructure
DDD also says that "higher" layers should depend on "lower" layers. Indeed, he stresses that when lower layers need to communicate upwards they should use callbacks or Observers. DDD is not the only source for this concept; it's pretty universal.

So what makes UI "higher" than "app"? Isn't it lower because it's a detail? In fact, isn't the highest level layer the domain layer? Certainly it is at the highest level of abstraction.

Perhaps the "highness" of a layer is not related to it's abstraction? Perhaps it's just it's position in the diagram above? If so, what justifies that position? The infrastructure, for example, seems to be at the same level of abstraction as the UI. Both are details. Both are replaceable without changing the nature of the app. Why are they at two different extremes.

Consider the Dependency Inversion Principle. One way to state this principle is that high level policy should not depend on low level detail. Yet the layering principle says that high level layers should depend upon low level layers. Clearly there is a mismatch here. Indeed, in the diagram above *everything* eventually depends upon the infrastructure; which is the the lowest level most detailed module of all. Could we be talking about two different dimensions of level? Could it be that a module is high level in one dimension and low level in another? What are these two dimensions?

These two dimensions are apparent in the work of Steve Mellor and Gregor Kiczales. Both of them have tried to address this problem in different ways. Gregor created the notion of aspects, so that infrastructure could be woven into an application, and could "Depend" on that application instead of the app depending on it. Indeed, AOP is a very interesting mechanism for conforming to DIP. Mellor created "translation" which is really just AOP at a higher level of abstraction. He created the infrastructure independent from the application, and then wrote another program called a translator to bind the two together. In both cases the infrastructure is orthogonal to the rest of the app.

The dilemma of level, and the two solutions, imply that we aren't thinking about this problem correctly. Infrastructure does not seem to be a layer at all. Somehow it is orthogonal to layers. Somehow it is both at a low level and at a high level. It's at a high level because it represents a policy for using the underlying platform. It's at a low level because it's a detail that most of the app wishes it didn't have to know about.

Perhaps the diagram should be drawn like this:

|UI|--->|APP|--->|DOM|   /----------------------/              A              |            |INF|
In other words, the UI, APP, and DOM are traditional layers, with the dependencies pointing in the direction of abstraction. (Apparently this means the the greater the abstraction, the lower the level. I still don't understand why that is.) And these layers are somehow independent of the infrastructure, and the infrastructure somehow manages to make itself felt whether through AOP, or Translation, or some other mechanism.

Without AOP, or Translation, we might draw the diagram this way:

|UI|--->|APP|--->|DOM|      |       |        |      V       V        V   |UIinf| |APPinf| |DOMinf|   /----------------------/              A              |            |INF|
In other words, each of the layers uses an abstract interface that represents it's infrastructure needs. It does not know what infrastructure it will be using. It simply states it's needs through an abstract interface and expects the infrastructure to implement that interface and supply the required functionality.

Presumably the |INF| module would implement those interfaces with some kind of adapter that would then forward the calls and messages to the infrastructure itself.

Thoughts?

转载地址:http://yqaoi.baihongyu.com/

你可能感兴趣的文章
放弃高考“打游戏”,别人25岁年入百万、我退役后回家修车
查看>>
蚂蚁员工持股平台管理权变更 马云持股降至34%
查看>>
拼多多:钟汉良出任“百亿补贴”星推官
查看>>
日赚1.2亿元,人均月薪5.5万元,蚂蚁集团招股书首亮相......
查看>>
索尼PS5或9月9日开启预订:售价挺香,钱包捂好
查看>>
七夕单日票房破5亿!创复工后单日票房新高
查看>>
2020年《闲鱼七夕报告》:男士在闲鱼搜索礼物是女生3倍
查看>>
真狠!10000mAh超大电池手机发布,真的神机...
查看>>
“9元课程”拼凑起来的暑假和K12在线教育企业的生死营销战
查看>>
会买单吗?李楠创办的怒喵科技发布新品牌了,推出潮流无线桌面产品
查看>>
小米集团公布新任CFO人选:系原瑞信亚太区高管
查看>>
开卡即绑卡!银行卡线下开户支持一键绑卡至微信支付
查看>>
一图看懂B站2020Q2财报
查看>>
苹果优化iPhone 12 Pro人脸识别,指纹识别还是别想了
查看>>
富士康备胎上位史
查看>>
Realme真我X7系列首发骁龙860? 副总裁辟谣:大家散了吧
查看>>
有人因微信被封跳楼?腾讯回应:公司员工未与坠楼者有过交流接触
查看>>
刘海、水滴、挖孔都靠边!中兴吐槽:不配全面屏
查看>>
小鹏汽车上市首日股价大涨41.47% 市值150亿美元
查看>>
坐地起价、山寨横行、人身骚扰:割韭菜的刀,还是“搬家公司”快
查看>>