I'm having a problem using the state pattern, I don't know how to check if a State
is of a certain instance without using instanceOf
(because that is considered a bad practice).
TCPConnection
holds a TCPState
object. Let's say I want to get all the TCPConnections
that have the state TCPEstablished
. How would I do this?
A way would be:
public List<TCPConnection> getAllEstablished() {
List<TCPConnection> list = new ArrayList<TCPConnection>();
for(TCPConnection tcp : allConnections) {
if(tcp.getState().instanceOf(TCPEstablished)) {
list.add(tcp);
}
}
return list;
}
But this uses instanceOf
and I would prefer not to use that. Are there any better ways? Or is my use of instanceOf
valid?
Aucun commentaire:
Enregistrer un commentaire