原文: https://www.sohamkamani.com/golang/command-pattern/
package main import "fmt" // The restaurant contains the total dishes and the total cleaned dishes type Restaurant struct { TotalDishes int CleanedDishes int } // `NewRestaurant` constructs a new restaurant instance with 10 dishes, // all of them being clean func NewResteraunt() *Restaurant { const totalDishes = 10 return &Restaurant{ TotalDishes: totalDishes, CleanedDishes: totalDishes, } } func (r *Restaurant) MakePizza(n int) Command { return &MakePizzaCommand{ restaurant: r, n: n, } } func (r *Restaurant) MakeSalad(n int) Command { return &MakeSaladCommand{ restaurant: r, n: n, } } func (r *Restaurant) CleanDishes() Command { return &CleanDishesCommand{ restaurant: r, } } type Command interface { execute() } // The MakePizzaCommand is a struct which contains // the number of pizzas to make, as well as the // restaurant as its attributes type MakePizzaCommand struct { n int restaurant *Restaurant } func (c *MakePizzaCommand) execute() { // Reduce the total clean dishes of the restaurant // and print a message once done c.restaurant.CleanedDishes -= c.n fmt.Println("made", c.n, "pizzas") } // The MakeSaladCommand is similar to the MakePizza command type MakeSaladCommand struct { n int restaurant *Restaurant } func (c *MakeSaladCommand) execute() { c.restaurant.CleanedDishes -= c.n fmt.Println("made", c.n, "salads") } type CleanDishesCommand struct { restaurant *Restaurant } func (c *CleanDishesCommand) execute() { // Reset the cleaned dishes to the total dishes // present, and print a message once done c.restaurant.CleanedDishes = c.restaurant.TotalDishes fmt.Println("dishes cleaned") } // A Cook comes with their list of commands as attributes type Cook struct { Commands []Command } // The executeCommands method executes all the commands // one by one func (c *Cook) executeCommands() { for _, c := range c.Commands { c.execute() } } func main() { // initialize a new resaurant r := NewResteraunt() // create the list of tasks to be executed tasks := []Command{ r.MakePizza(2), r.MakeSalad(1), r.MakePizza(3), r.CleanDishes(), r.MakePizza(4), r.CleanDishes(), } // create the cooks that will execute the tasks cooks := []*Cook{ &Cook{}, &Cook{}, } // Assign tasks to cooks alternating between the existing // cooks. for i, task := range tasks { // Using the modulus of the current task index, we can // alternate between different cooks cook := cooks[i%len(cooks)] cook.Commands = append(cook.Commands, task) } // Now that all the cooks have their commands, we can call // the `executeCommands` method that will have each cook // execute their respective commands for i, c := range cooks { fmt.Println("cook", i, ":") c.executeCommands() } }
--------------------
Command Pattern in Go (Golang)
November 27, 2019 · Written by Soham KamaniThis article will explain the command pattern, where to use it, and how to implement it in Go.
The command pattern, as the name suggests, is used when we want to create and execute “commands”. Different commands have their own implementation, but have the same steps for execution.
The Command Interface
The basic unit for implementing the command pattern is the Command interface:
type Command interface {
execute()
}
If the command can error out, the interface can contain an error return value as well:
type Command interface {
execute() error
}
标签:execute,dishes,restaurant,Pattern,Restaurant,cooks,Command,Go From: https://www.cnblogs.com/oxspirt/p/18068129