Here is an example using Go:
package mainimport ( "fmt")// list of commandstype Chef1 struct {}func (c *Chef1) Execute() { fmt.Printf("I cook seafood!\n")}type Chef2 struct {}func (c *Chef2) Execute() { fmt.Printf("I prepare side dishes\n")}// commandertype Desk struct {}func (d *Desk) CheckIn(orders []string) { for _, o := range orders { switch o { case "seafood": fallthrough case "blackpepper-lobsters": fallthrough case "fish-and-chips": chef1 := Chef1{} chef1.Execute() case "ceaser-salad": fallthrough case "cawamushi": chef2 := Chef2{} chef2.Execute() } }}// directortype Waiter struct {}func (w *Waiter) PlaceOrder(orders ...string) { d := Desk{} d.CheckIn(orders)}// restaurantfunc main() { w := Waiter{} w.PlaceOrder("seafood", "carbonara", "cawamushi", "fish-and-chips", )}// Output:// I cook seafood! // item: seafood// I prepare side dishes // item: cawamushi// I cook seafood! // item: fish-and-chipsThere are 3 layers of encapsulations here using the command pattern:
Note that: