So I did some refactoring and two of my classes now look exactly the same except for their constructors.
The classes wrap an API object that isn't very pretty, and add some functionality that belong at the edge of the 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
}
Does it make sense to push the common code between the two to an Abstract Base class, and have the subclasses only implement their constructors with the specialized option settings? It makes sense on paper, but something feels weird/counterintuitive about it. Am I missing a pattern here?
Possible DRYer Solution
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
}
EDIT
Static Factory Pattern is nice, but I think I may also add an Interface that includes the common methods I added.
I would make the BetterAPI
class also implement IBetterAPI
, which would only expose the methods I added wherever I declare the instance's type as 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);