简体   繁体   中英

How can an Enum class extend another external library class?

Now I have an existing class that I would like to refactor to be an Enum. The class currently extends another class which is from external library. As I still would like to benefit from some logics from that extended class meanwhile would like to refactor. How should it be done?

In Java, an enum class cannot extend another class whereas it can implement interface . Or is it already wrong for the idea of refactoring it to be an Enum? Let me show it in the example code below.

Assume an existing class Existing is extending another class Parent and the Parent class is from an external library and it is NOT an interface.

class Existing extends Parent{
    public static final Existing A = new Existing(...);
    ....
    public static final Existing Z = new Existing(...);

    public Existing(Srting attr1, String attr1){
        super(attr1, attr2);
    }

    public Existing(String attr1){
       super(attr1);
    }
}

The idea is to have those static final fields to be Enums, eg:

enum NewDesign{
     A(attr1, attr2),
     B(attr1),
     C(attr1, attr2)
     //...;
     //constructor etc.
     //...
}

and possibly when needed, with new extra attr added as below:

enum NewDesign{
  A(attr1, attr2, newAttr),
  B(attr1, newAttr),
  C(attr1, attr2, newAttr),
  //...
  //constructor etc.
  //...
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM