8
I have the classes Versao
, that a general version, VersaoFirmware
and VersaoSoftware
. In practice the user can add one or more versions to an equipment. However, at first, it is not yet known what type of version. Then I’ll add the versions with new Versao()
. When he finishes adding the versions, he must select which version type is, VersaoFirmware
or VersaoSoftware
.
What I tried to:
And I saw the schematic and I thought, "This is clearly an inheritance case. I have a superclass Versao
which has properties common to all version types. And I have the subclasses VersaoFirmware
and VersaoSoftware
who inherit from the superclass, for they have everything Versao
has, more or other 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 turn 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 Versao
have 50 properties, I will have to pass all by hand? And so also seems to me meaningless use of inheritance.
Complementing the title of the question, I tried to reverse the hierarchy of classes, solved the problem, but it is totally meaningless to leave exposed methods specific to the general version.
I read about Copy of Builder, that briefly creates a subclass from a superclass. But in the same way, you would have to pass all the properties manually.
How do I model this system? Is there any other strategy to solve this problem?
I asked a similar question in the OS: http://stackoverflow.com/questions/27134754/subclass-instance-to-superclass. Although I accept the answer, I asked this question here in more detail to see if this strategy is correct.
– Franchesco
Use a reflective method to copy properties. One of the best known classes is
BeanUtils.copyProperties
– utluiz
Ask the user to inform before the type of the version; then you will know the type of object to instantiate before the user starts entering the details of each version. If each version type has specific information, nor does it make sense for the user to inform the version type after informing the versions themselves.
– Caffé
@Caffé I agree with you. But you know how it is: "The boss asked". Then our biggest job is to try to convince the boss that this is not the best way. In case you don’t make it, let the programmer turn around.
– Franchesco
@Earendul You can also solve with application mechanics (maintain a data structure list and create business objects when you already have the minimum necessary information available), without corrupting modeling by a very specific usability need.
– Caffé
Related: "It is right to give greater preference to composition than inheritance?" I would say that this is a case where it is justified to give up the inheritance, although the language syntax does not help (if you want a single object with all the relevant properties, something you will have to do by hand). By the way, do you really need to create the class object before the user even finishes filling in the fields on the screen? Can’t do this at the end? And what happens if the user chooses the wrong type, he will have to cancel everything and start again?
– mgibsonbr
@mgibsonbr No need, so much so that I have already changed the mode, first the user enters the data and selects the type, only then he can add the versions of that type. However, I’m still in doubt in a case where I couldn’t select the type before.
– Franchesco