Let's say i have a rather complex state machine but i want to provide a Interface with simplifies it by combining some states into one.
My problem with the solution below is that I would have code duplication because of the two enums sharing some states which also exist in the Interface.
Can anybody give me a hint for a design pattern?
#include <iostream>
using namespace std;
enum State { state0, state1, state2, state3, state4, state5 };
enum SimpleState { simple_state0, // <-- Duplication
simple_state1, // <-- Duplication
simple_state2, // <-- Duplication
simple_state3, // <-- Duplication
stateSimple }; // < state4 or state5
class ComplexStateMachine
{
public:
State s_;
State getState()
{
return s_;
};
};
class SimpleInterface
{
public:
SimpleInterface(ComplexStateMachine* c)
{
this->complex_ = c;
};
SimpleState getSimpleState()
{
switch(this->complex_->getState())
{
case state0:
return simple_state0;
case state1:
return simple_state1;
case state2:
return simple_state2;
case state3:
return simple_state3;
case state4:
case state5:
return stateSimple;
}
};
private:
ComplexStateMachine* complex_;
};
int main() {
// Generate complex state machine
ComplexStateMachine* csm = new ComplexStateMachine();
csm->s_ = state0;
// Generate simple interface
SimpleInterface si(csm);
si.getSimpleState(); // <-- Should now return the simplified state
return 0;
}
Aucun commentaire:
Enregistrer un commentaire