Here is an example using Go:
package main
import (
"fmt"
)
// list of commands
type 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")
}
// commander
type 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()
}
}
}
// director
type Waiter struct {
}
func (w *Waiter) PlaceOrder(orders ...string) {
d := Desk{}
d.CheckIn(orders)
}
// restaurant
func 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-chips
There are 3 layers of encapsulations here using the command pattern:
Note that: