How should i represent a service running indefinitely using OOP ?
Suppose i have elevator class in which the service keeps the record of various conditions and execute other functions as required.
1 2 3 4 5 6 7 8 9 10 11 12 13
Class elevator{
private :
variables....
functions....
void service(){
while(1){
if(..)
do ...
else ...
}
}
};
Now , when the elevator is not in use (say at night) , i want to stop the service().
Destroying the elevator object does not seem to be correct way , so what is the standard way to do it?
Make elevator instance receive events from some sources (like button presses, and everything it needs to handle) and handle them depending on some state, say time of day. You might even send "Stop receining events" signal at night and "Begin receiving events" singnal in the morning. Busi wait loop is rarely a good idea.