Ale*_*ore 2 java oop refactoring dry
所以我做了一些重构,我的两个类现在看起来完全一样,除了它们的构造函数.
这些类包装了一个不太漂亮的API对象,并添加了属于API边缘的一些功能.
class A extends API {
public A {
this.APIOption = Option1;
this.AnotherAPIOption = Option2;
// a few more
}
public ArrayList<String> somethingTheAPIDoesntDo() {
// Do something the API doesn't provide but I use alot
}
// Other shared methods
}
class B extends API {
public B {
this.APIOption = Option3;
this.AnotherAPIOption = Option4;
// a few more
}
public ArrayList<String> somethingTheAPIDoesntDo() {
// Do something the API doesn't provide but I use alot
}
// Other shared methods
}
Run Code Online (Sandbox Code Playgroud)
将两者之间的公共代码推送到Abstract Base类是否有意义,并且子类只使用专门的选项设置实现其构造函数?它在纸上是有道理的,但有些东西对它感到怪异/违反直觉.我在这里错过了一个模式吗?
可能的DRYer解决方案
class A extends BetterAPIBase {
public A {
this.APIOption = Option1;
this.AnotherAPIOption = Option2;
// a few more
}
}
class B extends BetterAPIBase {
public B {
this.APIOption = Option3;
this.AnotherAPIOption = Option4;
// a few more
}
}
abstract class BetterAPIBase extends API {
public Better APIBase() {}
public ArrayList<String> somethingTheAPIDoesntDo() {
// Do something the API doesn't provide but I use alot
}
// Other methods
}
Run Code Online (Sandbox Code Playgroud)
编辑
静态工厂模式很好,但我想我也可以添加一个包含我添加的常用方法的接口.
我会让BetterAPI类也实现IBetterAPI,这只会在我声明实例的类型的地方公开我添加的方法IBetterAPI.
interface IBetterAPI{
public ArrayList<String> somethingTheAPIDoesntDo();
// other methods I added in BetterAPI
}
//somewhere else:
IBetterAPI niceApi = BetterAPI.createWithOptionSetA();
niceApi.somethingTheAPIDoesntDo();
// Can't do this, nice and hidden.
niceApi.somethingBaseAPIDoes(string uglyOptions, bool adNauseum);
Run Code Online (Sandbox Code Playgroud)
Pét*_*rök 13
让一个类具有参数化(可能是私有的)构造函数和多个静态工厂方法会不会更简单?
class BetterAPI extends API {
private BetterAPI(APIOption option, AnotherAPIOption anotherOption) {
this.APIOption = option;
this.AnotherAPIOption = anotherOption;
}
public static BetterAPI createWithOptionSetA() {
return new BetterAPI(Option1, Option2);
}
public static BetterAPI createWithOptionSetB() {
return new BetterAPI(Option3, Option4);
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
您的问题的核心似乎是您不能在同一个类中拥有多个无参数构造函数.静态工厂方法为此提供了一个很好的解决方案