I want to have 2 static functions called turnOn() and turnOff().
Say objectA calls turnOn(), then objectB calls turnOn(). Then objectA calls turnOff(), it should not turn off because objectB turned it on as well.
Also say objectA calls turnOn(), then objectB calls turnOn(). Then objectB calls turnOff(), it should not turn off because objectA hasn’t turned it off yet.
Finally, if objectA and objectB turn it on, it turns off when they both turn it off.
I was thinking keeping track of how many turnOn and TurnOff’s are called and make sure it matches, but thats not truly accurate because objectA can call turnOff() twice.
So whats the best sort of way to handle this?
Thanks!
The simplest method is as you describe: keep a count. If you’re really concerned with an object turning it off when it hasn’t turned it on, you can keep a list of all objects that have turned it on, and remove them from the list when they turn it off. If the list is empty, you can actually turn it off.