本文介绍了抽象不应该依赖细节。细节应该取决于抽象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几天里,我已经读了很多关于依赖注入/反转控制/反转的依赖。我认为,现在我对这个概念的理解好多了。但是我仍然没有从维基百科得到以下内容:

我了解部分高级模块不应该
依赖于低级模块。
但是,我对抽象和细节感到困惑。有人可以为我简化。谢谢。

解决方案

这意味着如果细节改变,他们不应该影响抽象。抽象是客户端查看对象的方式。正是在对象内部发生的事情并不重要。让我们以汽车为例,踏板和方向盘和变速杆是发动机内部发生的事情的抽象。他们不依赖于细节,因为如果有人改变我的旧发动机一个新的,我仍然可以驾驶汽车,而不知道引擎改变。



另一方面的细节必须符合抽象的说法。我不想实施突然使刹车将车速提高一倍的引擎。我可以用任何方式重新实施制动器,只要外部他们的行为一样。


In past couple of days, I have read about quite a lot about dependency injection/inversion of control/inversion of dependency. I think that, now my understanding of the concept is much better. But I still don't get the following from wikipedia:

I understand the part of High-level modules should notdepend on low-level modules. But, I am confused about abstractions and details.Can someone please simplify them for me. Thanks.

解决方案

It means that if the details change they should not affect the abstraction. The abstraction is the way clients view an object. Exactly what goes on inside the object is not important. Lets take a car for example, the pedals and steering wheel and gear lever are abstractions of what happens inside the engine. They do not depend on the details though because if someone changes my old engine for a new one I should still be able to drive the car without knowing that the engine changed.

The details on the other hand MUST conform to what the abstraction says. I would not want to implement an engine that suddenly causes the brakes to double the speed of the car. I can re-implement brakes any way I want as long as externally they behave the same way.

这篇关于抽象不应该依赖细节。细节应该取决于抽象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 23:42