I have a project where I work with generated classes for some web interfaces. Some of these classes are (almost) identical and most of the functionality I need them for only use the identical parts. Currently that functionality is implemented for each class separately. I want to refactor it in a way that I only have to implement the functionality once, independent of the used classes.
package a;
public class Domain{
//some attributes
public String getName(){//impl};
public String getType(){//impl};
public Boolean getDnsSec(){//impl};
//some setters
}
package b;
public class Domain{
//some attributes
public String getName(){//impl};
public String getType(){//impl};
public Boolean getIsSecured(){//impl};
public String getLastEditor(){//impl};
//some setters
}
package c;
public class Domain{
//some attributes
public String getName(){//impl};
public String getType(){//impl};
public Boolean getDnsSec(){//impl};
//some setters
}
My current idea is to use some kind of adapter pattern but I'm not sure if it's the right approach and if I'm doing it the correct way. The implementation would look something like this:
public abstract class DomainAdapter <Domain>{
private Domain domain;
public DomainAdapter(Domain domain){
this.domain = domain;
}
public abstract String getName();
public abstract String getType();
public abstract Boolean getDnsSec();
public Domain getDomain(){
return this.domain;
}
}
public DomainAAdapter extends DomainAdapter<a.Domain>{
public DomainGkAdapter(a.Domain domain) {
super(domain);
}
@Override
public String getName() {
return getDomain().getName();
}
@Override
public String getType(){
return getDomain().getType();
};
@Override
public Boolean getDnsSec(){
return getDomain().getDnsSec();
};
}
I'm wondering if this a good approach, if there is something better I could do or if it's maybe just wrong what I'm trying to do here. Thanks for the help and feedback in advance.
For clarification: I can not alter the Domain classes in package a, b, and c in any way. I think currently they are rebuilt from WSDL every time I build the project again and definitely need to be rebuilt if the corresponding WSDL gets updated. I do want a single point of access for these classes when I use them in my project though.
Instead of having three methods working with a.Domain, b.Domain or c.Domain as parameter, I want to have one function that works with a generalized domain parameter. Classic inheritance isn't possible since I cannot add a superclass that a.Domain, b.Domain and c.Domain inherit from.
Example:
//without adapter
public class DomainUser{
public void useDomain(a.Domain domain){
doSomething(domain.getName());
}
public void useDomain(b.Domain domain){
doSomething(domain.getName());
}
public void useDomain(c.Domain domain){
doSomething(domain.getName());
}
}
//with adapter
public class DomainUser{
public void useDomain(DomainAdapter domain){
doSomething(domain.getName());
}
}
To make things simpler you can assume that a.Domain, b.Domain and c.Domain are from different libraries I import and that I have no influence over.