架构师训练营第 1 期第 2 周作业

请描述什么是依赖倒置原则,为什么有时候依赖倒置原则又被称为好莱坞原则?
High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g. interfaces). 高层模块不应该依赖低层模块,大家都应该依赖抽象。
Abstractions should not depend on details. Details(concrete implementations) should depend on abstractions. 抽象不应该依赖于细节,细节应该依赖于抽象。
依赖倒置原则优化的例子

转换为

好莱坞原则是 Don't call me, I'll call you. 指的是不要来调用我,我回来调用你。
通过接口隔离优化 Cache 的设计,画出类图。


IClientCache 接口方法暴露给程序调用,包括 put, get, delete 方法。
IRemoteCache 接口的方法,通过 RemoteCache 实现。
MainCache 聚合了 RemoteCache,存储为私有变量,防止被其他程序调用。
评论