What would be a good approach to resume both the state & operation of an object?
For example, when transition to a particular state, the object will need to perform a certain operation.
The problem is when restoring the state directly via the object constructor: it is not a good practice to perform operation in the constructor. So at this point, only the state is restored, not the operation.
enum State { OPEN, CLOSED }
class Stuff {
State state;
Timer timer;
Stuff(State state, Timer timer) {
this.state = state;
this.timer = timer;
// It is not a good idea to call timer.start() here when the state = OPEN.
}
void open() {
state = OPEN;
timer.start();
}
void close() {
state = CLOSED;
}
}
Aucun commentaire:
Enregistrer un commentaire