I'd like some sage wisdom on avoiding downcasting in non-model code.
Here's an example
Let's say I'm writing an application which is quite MVC-ish, and in the model I have a state machine:
class StateDriven {
State current;
void Progress() {
current = current.Progress()
}
}
abstract class State {
public abstract State Progress();
}
class StateA : State {
// StateA Specific methods, properties, and Progress implementation.
}
class StateB : State {
// StateB Specific methods, properties, and Progress implementation.
}
Now Let's say I'm writing some UI code which wants to display a panel depending on the current state, with specific information for each state.
abstract class StatePanelUI<T> where T:State {
protected T state;
public abstract void Draw();
}
class StateAPanelUI : StatePanelUI<StateA> {
public override void Draw() {
// Logic for drawing a StateA
}
}
class StateBPanelUI : StatePanelUI<StateB> {
public override void Draw() {
// Logic for drawing a StateB
}
}
How can I know which type of panel to create without creating dependencies in the model? And once I create the panel, how can I inject the correct State subclass into it without downcasting?
I'm open to any methods/patterns/ideologies, but the key here is that the model can't depend on the visuals, because in my specific case I need to write several different UIs in several different environments.
Help appreciated!
Aucun commentaire:
Enregistrer un commentaire