💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ## 示例 ### 电视开关 <details> <summary>main.go</summary> ``` package main import "fmt" // 接收者接口 type device interface { on() off() } // 命令接口 type command interface { execute() } // 具体接口 type onCommand struct { device device } func (c *onCommand) execute() { c.device.on() } // 具体接口 type offCommand struct { device device } func (c *offCommand) execute() { c.device.off() } // 请求者 type button struct { command command } func (b *button) press() { b.command.execute() } type tv struct { isRunning bool } func (t *tv) on() { t.isRunning = true fmt.Println("Turning tv on") } func (t *tv) off() { t.isRunning = false fmt.Println("Turning tv off") } func main() { tv := &tv{} onCommand := &onCommand{ device: tv, } onButton := &button{ command: onCommand, } onButton.press() offCommand := &offCommand{ device: tv, } offButton := &button{ command: offCommand, } offButton.press() } ``` </details> <br /> 输出 ``` Turning tv on Turning tv off ```