1
0
Fork 0

实现适配器模式示例

This commit is contained in:
IvisTang 2025-12-12 00:28:08 +08:00
parent 357c4748f8
commit 64cc579e42
3 changed files with 185 additions and 0 deletions

63
go/adapter/main.go Normal file
View File

@ -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("=========== 适配器模式结束 ===========")
}

63
python/adapter/main.py Normal file
View File

@ -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()

59
ts/src/adapter/index.ts Normal file
View File

@ -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("=========== 适配器模式结束 ===========");
})();