How can an Enum class extend another external library class?
If the main change that you need is to create an enum to get rid of static final fields, the easiest way to go about it is to create a different, enum type with initialized instances:
enum ExistingEnum {
A("attr1", "attr2"),
Z("attr");
private final Existing existing;
ExistingEnum(String attr1) {
this.existing = new Existing(attr1);
}
ExistingEnum(String attr1, String attr2) {
this.existing = new Existing(attr1, attr2);
}
public Existing getExisting() {
return existing;
}
}
Depending on how you're using your Existing
class currently, you may still be able to change it to an enum. In the following example, I expose an instance of Parent
on the enum, assuming code using Existing
can be changed to call Existing.A.getParent().parentMethod()
...
enum Existing {
A("attr1", "attr2"),
Z("attr");
private final Parent existing;
Existing(String attr1) {
this.existing = new ParentImpl(attr1);
}
Existing(String attr1, String attr2) {
this.existing = new ParentImpl(attr1, attr2);
}
public Parent getParent() {
return existing;
}
// only needed if logic is overridden
private static class ParentImpl extends Parent {
public static final Existing A = "";
public static final Existing Z = "";
public ParentImpl(String attr1, String attr2) {
super(attr1, attr2);
}
public ParentImpl(String attr1) {
super(attr1);
}
}
}
I think you can use composition:
public class Parent {
private String attr1;
private String attr2;
public Parent(String attr1, String attr2) {
this.attr1 = attr1;
this.attr2 = attr2;
}
public void doSomething() {
// do something.
}
}
// compose the class Parent
public enum NewDesign {
A("attr1", "attr2"),
B("attr1", "attr2")
;
private Parent parent;
NewDesign(String attr1, String attr2) {
this.parent = new Parent(attr1, attr2);
}
public void func() {
parent.doSomething();
}
}
As I still would like to benefit from some logic from that extended class meanwhile would like to refactor.
Inheritance isn't the only one way you can benefit from the logic written in the library class. Composition (having a Parent
as a field in Existing
) may work.
Now I have an existing class that I would like to refactor to be an Enum.
Why enum? Your Existing
class has two constructors meaning that you expect new instances to be created regardless of the already defined objects A
...Z
. It isn't the idea behind enums.
and possibly when needed, with new extra attr added
It can be tedious to refactor all the constants when a new attribute emerges. You are going to modify the class as soon as a new attribute is added. It doesn't look sound to me, and it kinda violates the open-closed principle - you aren't really open for extension with enums.