我正在学习 SOLID 原则。在学习“依赖倒置原则”时发现类应该依赖于接口而不是具体类。
这是否意味着不允许组合?另外,聚合是否与 DIP 相同?
作品:
class HPDesktop {
private BluetoothMouse bluetoothMouse;
private BluetoothKeyboard bluetoothKeyboard;
public HPDesktop(){
bluetoothMouse = new BluetoothMouse();
bluetoothKeyboard = new BluetoothKeyboard();
}
}
DIP:(这似乎是一种聚合)
class HPDesktop {
private Mouse mouse;
private Keyboard keyboard;
public HPDesktop(Mouse mouse, Keyboard keyboard){
this.mouse = mouse;
this.keyboard = keyboard;
}
}