1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package main
import "fmt"
func main() { a := NewAdapter() a.Pay("张三", 12, "aliyun") a.Pay("李四", 200, "wx") }
type AliyunPayInterface interface { Pay(name string, payment float64) }
type AliyunPay struct{}
func (a AliyunPay) Pay(name string, payment float64) { fmt.Printf("用户【%s】支付宝支付金额【%f】元 \n", name, payment) }
type WxPayInterface interface { Pay(name string, payment float64) } type WxPay struct { }
func (w WxPay) Pay(name string, payment float64) { fmt.Printf("用户【%s】微信支付金额【%f】元 \n", name, payment) }
type Apadter struct { AliyunPayInterface WxPayInterface }
func (a Apadter) Pay(name string, payment float64, channel string) { if channel == "aliyun" { a.AliyunPayInterface.Pay(name, payment) } else if channel == "wx" { a.WxPayInterface.Pay(name, payment) } else { fmt.Printf("不支持的支付渠道") } }
func NewAdapter() Apadter { return Apadter{ AliyunPay{}, WxPay{}, } }
|