I have the classes Versao
, which is a general version, VersaoFirmware
and VersaoSoftware
. In practice the user can add one or more versions to an equipment. However, at the first moment, it is not yet known what kind of version. So I'm adding the versions with new Versao()
. When it finishes adding the versions, it should select the type of versions, VersaoFirmware
or VersaoSoftware
.
What I've tried:
I have a% super_class which has properties common to all version types, and I have the subclassesVersao
and% with% of them inheriting from the superclass, because they have everything that VersaoFirmware
has, plus one or another different method. "
class Versao {
private int id;
private String name;
public Versao() {}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class VersaoFirmware extends Versao {
private String outraCoisa;
public VersaoFirmware() {}
public String fazOutraCoisa() {
return outraCoisa;
}
}
class VersaoSoftware extends Versao {
private String outraCoisa;
public VersaoSoftware () {}
public String fazOutraCoisa() {
return outraCoisa;
}
}
However, I came across the following problem: after the user selects the version type, how do I convert the general version, to the specific version? Should I create new instances of each type and pass the attributes manually? For example:
Versao versao = new Versao();
versao.setName("Versao");
VersaoSoftware versaoSoft = new VersaoSoftware();
versaoSoft.setName(versao.getName());
versaoSoft.setId(versao.getId()); // ....
But what if VersaoSoftware
has 50 properties, do I have to manually pass them all? And so I also find the use of inheritance meaningless.
Complementing the title of the question, I tried to invert the class hierarchy, solved the problem, but it is totally meaningless to leave exposed specific methods for the general version.
I read about Builder Copy , which briefly creates a subclass from a superclass. But in the same way, you would have to pass all properties manually.
How do I model this system? Are there any other strategies to solve this problem?