您可以创建一个枚举 SynapseState,它象征着类 Synapse 的状态。将状态信息嵌入枚举的优点是您可以在 switch 语句中使用它。任何与 Synapse 状态相关的不变信息都可以包含在带有抽象方法的枚举中。检查下面的示例我从问题示例代码中可以得到的任何信息中所做的事情
enum SynapseState {
CONNECTED(0) {
@Override
List<StateParameter> getStateParameters() {
return null;
}
}, ACTIVE(1) {
@Override
List<StateParameter> getStateParameters() {
return null;
}
}, PREVIOUS_ACTIVE(2) {
@Override
List<StateParameter> getStateParameters() {
return null;
}
};
private int value;
private SynapseState(int value) {
this.value = value;
}
abstract List<StateParameter> getStateParameters();
};
class SynapseMachine{
List<Synapse> connectedSynapses;
List<Synapse> activeSynapses;
List<Synapse> previousActiveSynapses;
List<Synapse> synapses;
public void addSynapse(Synapse synapse)
{
Synapse newSynapse = new Synapse(synapse.getInputCell());
this.synapses.add(newSynapse);
switch(synapse.getState()){
case CONNECTED:
this.connectedSynapses.add(newSynapse);
break;
case ACTIVE:
this.activeSynapses.add(newSynapse);
break;
case PREVIOUS_ACTIVE:
this.previousActiveSynapses.add(newSynapse);
break;
default:
throw new IllegalStateException();
}
}
}
class Synapse{
public Synapse(Object o){}
SynapseState getState(){
// TODO Auto-generated method stub
return null;
}
public Object getInputCell() {
// TODO Auto-generated method stub
return null;
}
}
class StateParameter{
}