注:本文已发布超过一年,请注意您所使用工具的相关版本是否适用
序
笔记

代码实现
假设我现在有一个运维系统,需要分别调用阿里云和 AWS 的 SDK 创建主机,两个 SDK 提供的创建主机的接口不一致,此时就可以通过适配器模式,将两个接口统一。
PS:AWS 和 阿里云的接口纯属虚构,没有直接用原始的 SDK,只是举个例子
Code
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
| package adapterimport "fmt" // ICreateServer 创建云主机 type ICreateServer interface { CreateServer(cpu, mem float64) error } // AWSClient aws sdk type AWSClient struct{} // RunInstance 启动实例 func (c *AWSClient) RunInstance(cpu, mem float64) error { fmt.Printf("aws client run success, cpu: %f, mem: %f", cpu, mem) return nil } // AwsClientAdapter 适配器 type AwsClientAdapter struct { Client AWSClient } // CreateServer 启动实例 func (a *AwsClientAdapter) CreateServer(cpu, mem float64) error { a.Client.RunInstance(cpu, mem) return nil } // AliyunClient aliyun sdk type AliyunClient struct{} // CreateServer 启动实例 func (c *AliyunClient) CreateServer(cpu, mem int) error { fmt.Printf("aws client run success, cpu: %d, mem: %d", cpu, mem) return nil } // AliyunClientAdapter 适配器 type AliyunClientAdapter struct { Client AliyunClient } // CreateServer 启动实例 func (a *AliyunClientAdapter) CreateServer(cpu, mem float64) error { a.Client.CreateServer(int(cpu), int(mem)) return nil }
|
单元测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package adapterimport ( "testing" ) func TestAliyunClientAdapter_CreateServer(t *testing.T) { // 确保 adapter 实现了目标接口 var a ICreateServer = &AliyunClientAdapter{ Client: AliyunClient{}, } a.CreateServer(1.0, 2.0) } func TestAwsClientAdapter_CreateServer(t *testing.T) { // 确保 adapter 实现了目标接口 var a ICreateServer = &AwsClientAdapter{ Client: AWSClient{}, } a.CreateServer(1.0, 2.0) }
|
关注我获取更新
猜你喜欢