I m facing the following code:
public class BaseGroup {
private Group1 group1;
private Group2 group2;
private Group3 group3;
public void setGroup (IGroup group) {
if(group instanceof Group1) {
setGroup1((Group1) group);
} else if(group instanceof Group2) {
setGroup2((Group2) group);
} else {
setGroup3((Group3) group);
}
}
public Group1 getGroup1() {
return group1;
}
public void setGroup1(Group1 group1) {
this.group1 = group1;
}
public Group2 getGroup2() {
return group2;
}
public void setGroup2(Group2 group1) {
this.group2 = group2;
}
public Group3 getGroup3() {
return group3;
}
public void setGroup3(Group3 group1) {
this.group3 = group3;
}
}
And the BaseGroup class is used in this way.
BaseGroup baseGroup = New BaseGroup();
basegroup.setGroup(group);
我的问题是这一链条,即要求各组装组标的“instanceof s”。 这样做的更好办法是什么?