F
François Grondin
Hi everyone
I have the following problem. I'm developing a factory method that creates
objects from a list. This list contains objects that implements my interface
IRegulatedNME. Three classes implements this interface : Gate, Pump and
Weir. My factory method looks like this (I hope this code is clear enough) :
private List<AbstractConstraint> create(List<IRegulatedNME> aRegNMEList)
{
List<AbstractConstraint> ret = new ArrayList<AbstractConstraint>();
for (IRegulatedNME regNME : aRegNMEList)
{
if (regNME.isGloballyCtrl())
{
ret.add(new GloballyCtrlConstraint(regNME);
}
else if (regNME.isLocallyCtrl())
{
ret.add(new LocallyCtrlConstraint(regNME);
}
else if (regNME.isUnctrl())
{
if (regNME instanceof Gate)
{
ret.add(new UnctrlGateConstraint((Gate) regNME);
}
else if (regNME instanceof Pump)
{
ret.add(new UnctrlPumpConstraint((Pump) regNME);
}
else // if (regNME instanceof Weir)
{
ret.add(new UnctrlWeirConstraint((Weir) regNME);
}
}
}
return ret;
}
As you could see, the cases "regNME.isGloballyCtrl()" or
"regNME.isLocallyCtrl()" apply to the abstract object. When
"regNME.isUnctrl()", then the constraints to be added depends on the type of
regNME (Gate, Pump or Weir). The list "aRegNMEList" may contain gates, pumps
or weirs at the same time. Is there a way to avoid the use of "instanceof"
in that situation? Could the generics be useful to solve this problem? Is
there a way to filter my list to extract only one type at a time (without
using "instanceof" of course)?
I googled "replace instanceof java" and "filter collection java" but was not
satisfied with the answers.
Thanks in advance for your replies.
François
I have the following problem. I'm developing a factory method that creates
objects from a list. This list contains objects that implements my interface
IRegulatedNME. Three classes implements this interface : Gate, Pump and
Weir. My factory method looks like this (I hope this code is clear enough) :
private List<AbstractConstraint> create(List<IRegulatedNME> aRegNMEList)
{
List<AbstractConstraint> ret = new ArrayList<AbstractConstraint>();
for (IRegulatedNME regNME : aRegNMEList)
{
if (regNME.isGloballyCtrl())
{
ret.add(new GloballyCtrlConstraint(regNME);
}
else if (regNME.isLocallyCtrl())
{
ret.add(new LocallyCtrlConstraint(regNME);
}
else if (regNME.isUnctrl())
{
if (regNME instanceof Gate)
{
ret.add(new UnctrlGateConstraint((Gate) regNME);
}
else if (regNME instanceof Pump)
{
ret.add(new UnctrlPumpConstraint((Pump) regNME);
}
else // if (regNME instanceof Weir)
{
ret.add(new UnctrlWeirConstraint((Weir) regNME);
}
}
}
return ret;
}
As you could see, the cases "regNME.isGloballyCtrl()" or
"regNME.isLocallyCtrl()" apply to the abstract object. When
"regNME.isUnctrl()", then the constraints to be added depends on the type of
regNME (Gate, Pump or Weir). The list "aRegNMEList" may contain gates, pumps
or weirs at the same time. Is there a way to avoid the use of "instanceof"
in that situation? Could the generics be useful to solve this problem? Is
there a way to filter my list to extract only one type at a time (without
using "instanceof" of course)?
I googled "replace instanceof java" and "filter collection java" but was not
satisfied with the answers.
Thanks in advance for your replies.
François