jeudi 9 septembre 2021

XmlElement common for multiple classes

I have a design question: there are 3 classes of type

@XmlRootElement( name="a-class" )
@XmlAccessorType(XmlAccessType.FIELD)
class A {
 
private int commonInteger;

@XmlElementWrapper(name="commonCollection")
@XmlElement(name="commonCollection")
private Collection<Integer> commonCollection;

//Some class-specific variables 

//Getters and Setters

}
@XmlRootElement( name="b-class" )
@XmlAccessorType(XmlAccessType.FIELD)
class B {
private int commonInteger;

@XmlElementWrapper(name="commonCollection")
@XmlElement(name="commonCollection")
private Collection<Integer> commonCollection;

//Some class-specific variables 

//Getters and Setters

}
@XmlRootElement( name="c-class" )
@XmlAccessorType(XmlAccessType.FIELD)
class C {
private int commonInteger;

@XmlElementWrapper(name="commonCollection")
@XmlElement(name="commonCollection")
private Collection<Integer> commonCollection;

//Some class-specific variables 

//Getters and Setters

}

Now obviously some of the common code is replicated. I wanted to refactor this code - so I thought of creating another class and extending it in all 3 implementations. But not sure if this is an optimal design choice and how can I extract out the fields having annotations. Can someone help?

class Common {
      private int commonInteger;

}

So it will be something like class A extends Common, class B extends Common, class C extends Common. I am refraining from using interfaces as I do not want the members to be final.

Aucun commentaire:

Enregistrer un commentaire