package westworldwithmessaging import ( "fmt" "sync" ) // eatStew is the Go version of the (not yet pasted) singleton class // EatStew from the book. Same pattern as every other state: // unexported type, one shared instance, reached only through the // function below. type eatStew struct{} var ( eatStewInstance *eatStew eatStewOnce sync.Once ) // EatStew is the Go stand-in for "EatStew::Instance()". func EatStew() *eatStew { eatStewOnce.Do(func() { eatStewInstance = &eatStew{} }) return eatStewInstance } var _ State[Miner] = (*eatStew)(nil) // The book never pasted this state's actual source - it only ever // referenced EatStew::Instance() as a switch target. This whole // implementation is reverse-engineered from a sample run of the // finished program, which shows this exact sequence: Bob enters // (one line), eats (one line, then immediately heads back to // whatever he was doing - a single-tick state, same shape as // QuenchThirst), and leaving prints one more line before he's back // in GoHomeAndSleepTilRested. // Enter announces that dinner smells good. func (s *eatStew) Enter(m *Miner) { fmt.Printf("\n%s: Smells reaaal goood, Elsa!", GetNameOfEntity(m.ID())) } // Execute is the entire meal: announce it, then head back to // whatever state he was in before dinner interrupted him. The sample // output never shows more than one "Tastes real good too!" line, so // - like QuenchThirst - this doesn't loop across multiple ticks. func (s *eatStew) Execute(m *Miner) { fmt.Printf("\n%s: Tastes real good too!", GetNameOfEntity(m.ID())) m.ChangeState(GoHomeAndSleepTilRested()) } // Exit thanks Elsa for the meal on the way out. func (s *eatStew) Exit(m *Miner) { fmt.Printf("\n%s: Thank ya li'l lady. Ah better get back to whatever ah wuz doin'", GetNameOfEntity(m.ID())) } // OnMessage is required now that State[T] includes it. The sample // output doesn't show Bob reacting to any message while eating, so // false ("not handled") is the correct default. func (s *eatStew) OnMessage(m *Miner, msg Telegram) bool { return false }