gpt4 book ai didi

c++ - 无法理解 Robert Martin 的 ISP 文章中的 "contradiction"

转载 作者:行者123 更新时间:2023-12-01 14:57:44 26 4
gpt4 key购买 nike

我阅读了 Robert Martin 关于接口(interface)隔离原则的文章 here .在文章的最后,在解决 ATM UI 架构的问题时,他说:

Consider also that each different transaction that the ATM can perform is encasulated as a derivative of the class Transaction. Thus we might have classes such as DepositTransaction, WithdrawlTransaction, TransferTransaction, etc. Each of these objects issues message to the UI. For example, the DepositTransaction object calls the RequestDepositAmount member function of the UI class. Whereas the TransferTransaction object calls the RequestTransferAmount member function of UI. This corresponds to the diagram in Figure 5.

Notice that this is precicely the situation that the ISP tells us to avoid. Each of the transactions is using a portion of the UI that no other object uses. This creates the possibility that changes to one of the derivatives of Transaction will force coresponding change to the UI, thereby affecting all the other derivatives of Transaction, and every other class that depends upon the UI interface.



所以我们有以下情况:如果 Transaction之一的导数发生变化,则 UI已更改 以及使用 UI 的任何其他类也变了。

然后通过以下更改解决了该问题:

This unfortunate coupling can be avoided by segregating the UI interface into induvidual abstract base classes such as DepositUI, WithdrawUI and TransferUI. These abstract base classes can then be multiply inherited into the final UI abstract class. Figure6 and Listing 6 show this model.



但接下来罗伯特·马丁说:

It is true that, whenever a new derivative of the Transaction class is created, a coresponding base class for the abstract UI class will be needed. Thus the UI class and all its derivatives must change. However, these classes are not widely used. Indeed, they are probably only used by main, or whatever process boots the system and creates the concrete UI instance. So the impact of adding new UI base classes is contained.



这就是问题所在: UI 怎么可能?的改变,但没有其他类也改变了?毕竟,如果某种 TransactionX使用 XUIXUIUI 的父类(super class)和 UI被更改(因为某些 ZUI ),然后(就我而言)编译器需要重新编译所有使用 XUI 的类同样,因为 vtable(就 C++ 而言)或者某些函数基地址已因 UI 的更改而更改.有人可以帮我弄干净吗?

最佳答案

if some kind of TransactionX uses XUI and XUI is superclass of UI and UI is changed, then (as far as i'm concerned) compiler needs to recompile all the classes that use XUI too



是的,但在这种情况下,只有 TransactionX取决于 XUI .所有其他 TransactionYYUI不受影响,也不需要重新编译。

because vtable (in terms of C++) or maybe some function base addresses have been changed by change of UI.



你会重新编译 main (或文本中的 ui_globals.cc)这是 X/Y/Z UI 的地址获取接口(interface)传递给 Transaction X/Y/Z实例。

关于c++ - 无法理解 Robert Martin 的 ISP 文章中的 "contradiction",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61632204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com