From 64cc579e42a63860fab6f4f182caa33ca5b98989 Mon Sep 17 00:00:00 2001 From: IvisTang Date: Fri, 12 Dec 2025 00:28:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E9=80=82=E9=85=8D=E5=99=A8?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go/adapter/main.go | 63 +++++++++++++++++++++++++++++++++++++++++ python/adapter/main.py | 63 +++++++++++++++++++++++++++++++++++++++++ ts/src/adapter/index.ts | 59 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 go/adapter/main.go create mode 100644 python/adapter/main.py create mode 100644 ts/src/adapter/index.ts diff --git a/go/adapter/main.go b/go/adapter/main.go new file mode 100644 index 0000000..44b339e --- /dev/null +++ b/go/adapter/main.go @@ -0,0 +1,63 @@ +package main + +/* +实例 +汤宝宝有一个苹果手机,手机原配lightning接口,为了连接Type-C接口的充电器,他需要一个转换器(适配器)来实现这个功能。 +*/ + +import "fmt" + +// 充电器类 +type Charger struct{} + +func (c *Charger) Connect(typec TypeCInterface) { + typec.TypeCCharging() + fmt.Println("手机开始充电!") + fmt.Println() +} + +// Type-C充电接口的接口 + +type TypeCInterface interface { + TypeCCharging() +} + +// 具体实现类 +type TypeC struct{} + +func (t *TypeC) TypeCCharging() { + fmt.Println("Type-C接口连接中...") +} + +type Lightning struct{} + +func (l *Lightning) LightningCharging() { + fmt.Println("Lightning接口连接中...") +} + +// 适配器类 +type LightningToTypeCAdapter struct { + lightning *Lightning +} + +func (l *LightningToTypeCAdapter) TypeCCharging() { + fmt.Println("Type-C适配器转换中...") + l.lightning.LightningCharging() +} + +func main() { + charger := &Charger{} + lightning := &Lightning{} + typec := &TypeC{} + + fmt.Println("============= 适配器模式 =============") + + fmt.Println("使用Type-C充电器给小米手机充电:") + charger.Connect(typec) + + fmt.Println("使用Lightning接口转换器给苹果手机充电:") + adapter := &LightningToTypeCAdapter{lightning: lightning} + charger.Connect(adapter) + + fmt.Println("=========== 适配器模式结束 ===========") +} diff --git a/python/adapter/main.py b/python/adapter/main.py new file mode 100644 index 0000000..812cce6 --- /dev/null +++ b/python/adapter/main.py @@ -0,0 +1,63 @@ +""" +实例 +汤宝宝有一个苹果手机,手机原配lightning接口,为了连接Type-C接口的充电器,他需要一个转换器(适配器)来实现这个功能。 +""" + +from abc import ABC, abstractmethod + + +# 充电器类 +class Charger: + def connect(self, type_c: "TypeCInterface"): + type_c.type_c_charging() + print("手机开始充电!\n") + + +# Type-C充电接口的接口 +class TypeCInterface(ABC): + @abstractmethod + def type_c_charging(self): + pass + + +# 具体实现类 + + +class TypeC(TypeCInterface): + def type_c_charging(self): + print("Type-C接口连接中...") + + +class Lightning: + def lightning_charging(self): + print("Lightning接口连接中...") + + +# 适配器类 +class LightningToTypeCAdapter(TypeCInterface): + def __init__(self, lightning: Lightning): + self.lightning = lightning + + def type_c_charging(self): + print("Type-C适配器转换中...") + self.lightning.lightning_charging() + + +def main(): + print("============= 适配器模式 =============") + charger = Charger() + + print("使用Type-C充电器给小米手机充电:") + typec = TypeC() + charger.connect(typec) + + print("使用Lightning接口转换器给苹果手机充电:") + lightning = Lightning() + adapter = LightningToTypeCAdapter(lightning) + charger.connect(adapter) + + print("=========== 适配器模式结束 ===========") + + +if __name__ == "__main__": + main() diff --git a/ts/src/adapter/index.ts b/ts/src/adapter/index.ts new file mode 100644 index 0000000..90d0449 --- /dev/null +++ b/ts/src/adapter/index.ts @@ -0,0 +1,59 @@ +/* +实例 +汤宝宝有一个苹果手机,手机原配lightning接口,为了连接Type-C接口的充电器,他需要一个转换器(适配器)来实现这个功能。 +*/ + +// 充电器类 +class Charger { + connect(typec: TypeCInterface) { + typec.typeCCharging(); + console.log("手机开始充电!\n"); + } +} + +// Type-C充电接口的接口 +interface TypeCInterface { + typeCCharging(): void; +} + +// 具体实现类 +class TypeC implements TypeCInterface { + typeCCharging(): void { + console.log("Type-C接口连接中..."); + } +} + +class Lightning { + lightningCharging(): void { + console.log("Lightning接口连接中..."); + } +} + +// 适配器类 +class LightningToTypeCAdapter implements TypeCInterface { + private lightning: Lightning; + constructor(lightning: Lightning) { + this.lightning = lightning; + } + typeCCharging(): void { + console.log("Type-C适配器转换中...") + this.lightning.lightningCharging(); + } +} + +(function () { + const charger = new Charger(); + + console.log("============= 适配器模式 ============="); + + console.log("使用Type-C充电器给小米手机充电:"); + const typec = new TypeC(); + charger.connect(typec); + + console.log("使用Lightning接口转换器给苹果手机充电:"); + const lightning = new Lightning(); + const adapter = new LightningToTypeCAdapter(lightning); + charger.connect(adapter); + + console.log("=========== 适配器模式结束 ==========="); +})();