适配器模式——设计模式
https://github.com/youngyangyang04/kama-DesignPattern/blob/main/DesignPattern/6-%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F.md
package main
import (
"fmt"
)
// TypeC接口
type TypeC interface {
ChargeWithTypeC(*Usb)
}
// Usb接口
type Usb interface {
ChargeWithUsb()
}
// 新电脑类型
type NewComputer struct {
}
func (np *NewComputer) ChargeWithTypeC() {
fmt.Println("TypeC")
}
// 现有的usb类型
type Usbtruct struct {
}
func (tcs *Usbtruct) ChargeWithUsb() {
}
// 适配器
type TypeCToUsbAdaptar struct {
usbOjb *Usbtruct
}
func (typecAdaptar *TypeCToUsbAdaptar) ChargeWithUsb() {
typecAdaptar.usbOjb.ChargeWithUsb()
fmt.Println("USB Adapter")
}
func main() {
var n int
fmt.Scan(&n)
var usbObj = new(Usbtruct)
var newComputer = new(NewComputer)
var typeCToUsbAdaptar = new(TypeCToUsbAdaptar)
typeCToUsbAdaptar.usbOjb = usbObj
for n > 0 {
var t int
fmt.Scan(&t)
if t == 1 {
newComputer.ChargeWithTypeC()
} else {
typeCToUsbAdaptar.ChargeWithUsb()
}
n--
}
}