简体   繁体   中英

java how to share field and method between classes

I have some BaseX pojo class ex:

public class Base1 {
    private String base1Field1;
    private String base1Field2;
    // getter & setter
}
public class Base2 {
    private String base2Field1;
    private String base2Field2;
    // getter & setter
}

And some DomainVo pojo class, it has some field & method from BaseX class ex:

//from base1
public class Domain1Vo {
    private String domain1Field1;
    private String base1Field1;
    private String base1Field2;
    // getter & setter
}
//from base1 & base 2
public class Domain2Vo {
    private String domain2Field1;
    private String base1Field1;
    private String base1Field2;
    private String base2Field1;
    private String base2Field2;
    // getter & setter
}

I don't want copy paste those fields from BaseX class to my domain object, but I can't use extends Base1, Base2 because java don't allow it.

I don't actually use BaseX in my code, I define them just want a single place to maintain those field.

update: I can't change my domain object structure, because it's come from/to other service as json. Also I need use it to generate a swagger doc(I use springfox). This why I need both field & method.

======== some research I did

I found a lib called lombok , it has a feature name @Delegate https://projectlombok.org/features/experimental/Delegate

//from base1
public class Domain1Vo {
    private String domain1Field1;
    @Delegate
    private Base1 base1;
    // getter & setter
}

It can generate method from BaseX class, but not field.

Composition is what you want. Inheritance is wildly overused (also getters and, especially, setters).

The base classes become components. (Use better names! Even "base" isn't a useful word to use in a class name.)

public final class Component1 {
    private String component1Field1;
    private String component1Field2;
    // domain methods
}
public final class Component2 {
    private String component2Field1;
    private String component2Field2;
}

public final class Domain1Vo {
    private String domain1Field1;
    private final Component1 component1;
    public Domain1Vo(
        Component1 component1
    ) {
        // Or construct.
        this.component1 =
           Objects.requireNonNull(component1);
    }
    // domain methods
}
//from base1 & base 2
public final class Domain2Vo {
    private String domain2Field1;
    private final Component1 component1;
    private final Component2 component2;
    public Domain2Vo(
        Component1 component1,
        Component2 component2,
    ) {
        // Or construct.
        this.component1 =
           Objects.requireNonNull(component1);
        this.component2 =
           Objects.requireNonNull(component2);
    }
    // domain methods
}

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