我有下面的代碼,我想向類impl中添加一個新的構造函數,而不會對現有代碼進行任何更改。我是否應該使用適配器並編寫自己的包裝器實現,或者有什麼方法可以以某種方式執行,我錯過了嗎? 特別ClassImpl領域Class2中需要(使用適配器模式?)設計問題的最佳方式
public interface Class1 {
void customise(Configuration configuration);
}
public interface Class2 {
void customise(DeprecatedConfiguration configuration);
}
class DeprecatedConfiguration extends Configuration {
}
public class ClassImpl {
private final Class2 customizer;
private final DeprecatedConfiguration config;
// want to deprecate this
public ClassImpl(final Class2 pCustomiser) {
customizer = pCustomiser;
config = new DeprecatedConfiguration();
}
// want to add this
//public ClassImpl(final Class1 pCustomiser) {
// customizer = pCustomiser;
// config = new Configuration();
//}
public void someMethod() {
customizer.customise(config);
}
}
我同意,但我不得不不情願地走黑客道路。謝謝。 – Scorpion