I think you should keep things simple in this case. The three switch system is a challenge with a solution and this Puzzle
has two states, currently solved correctly and currently unsolved (or solved and then reverted to unsolved for some reason [timer?]). So you have a Boolean
value and you need to store it somewhere in your game so the door could get it and behave accordingly.
One generic solution would be to make the Door an observer of the Puzzle. So the Door listens to the Puzzle and knows when to open up or close. This could be problematic however since that Door does not exist at all during the puzzle phase. This means the state of the Puzzle resolution needs to be stored somewhere and passed back to the Door somehow.
This is why I would suggest a "delayed Event" system where you store a Hash
aka Associative array and send a "message" through that Hash, for instance:
//In the Puzzle class,
{
String threeSwitchPuzzle = "The puzzle with three switches";
}
// After completion
delayedEvent(threeSwitchPuzzle, success);
// Which translates to
Hash[threeSwitchPuzzle] = true;
// Construct the Door with the appropriate puzzle String as a member
// Then when the `Door` is created
if (getDelayedEvent(door.puzzle)) // behave accordingly
I know this is similar to the obvious solution but if you design it in a way that is legible and maintainable it should work for you and save you the trouble of overthinking this. You simply need the state stored somwhere and retrieved from that place later.
static memory allocation
can satisfy this requirement.singleton
is one of possible implementations. \$\endgroup\$