I have a project that can print some data depending on some states and modes that the app is in.
enum Mode {
mode1 = 0,
mode2
};
enum State {
state1 = 0,
state2
};
Data can be printed in multiple ways, here is some examples:
1. name1 value1 name2 value2 name3 value3
2. value1 value2 value3
3. name1 : value1.
name2 : value2
name3 : value3
I tried to make a class with the ostream operator overload:
class Formater {
public:
Formater(.....) { ... } // pass name, value, mode, state here.
virtual void print_State1_Mode1(ostream& os) { }
virtual void print_State1_Mode2(ostream& os) { }
virtual void print_State2_Mode1(ostream& os) { }
virtual void print_State2_Mode2(ostream& os) { }
friend std::ostream& operator << (std::ostream& os, const Formater& f) {
if (state1 & mode1) {
print_State1_Mode1(os);
}
else if(state1 & mode2) {
print_State1_Mode2(os);
}
else if(state2 & mode1) {
print_State2_Mode1(os);
}
else {
print_State2_Mode2(os);
}
return os;
}
};
This will be used for some commands and every command depending on the state and mode can have different format to print text.
So if my object cannot satisfy a command, I inherit from it and create a new one and override one of the virtual methods, depending for which mode and state I need a new format.
So I can end up with many more Formater objects (Formater1, Formater2....).
I am not 100% satisfied with this approach. Does anyone have a better design or ways I can improve my current design ?
Aucun commentaire:
Enregistrer un commentaire