[go] 适配器模式

适配器模式

将一个类的接口,转换成客户期望的另一个接口。适配器让原来接口不兼容的类可以合作无间。

模型说明

对象适配器

  • Client:是包含当前程序业务逻辑的类

  • 客户端代码只需通过与适配器交互即可,无需与具体的适配器耦合。因此,你可以向程序中添加新类型的适配器而无需修改已有代码。这在服务类的接口被更改或替换时很有用:你无需修改客户端代码就可以创建新的适配器类。

  • Client Interface:描述了其他类与客户端代码合作时必须遵循的协议

  • Service:有一些功能类(通常来自第三方或遗留系统)。客户端与其接口不兼容,因此无法直接调用其功能。

  • Adapter:是一个可以同时与客户端和服务端交互的类:它是实现客户端接口的同时封装了服务对象。适配器接受客户端通过适配器接口发起的调用,并将其转换为被封装服务对象的调用。

类适配器

Adapter:不需要封装任何对象,因为它同时继承了客户端和服务端的行为。适配功能在重写的方法中完成。最后生成的适配器可替代已有的客户端类进行使用。

优缺点

1.优点

  • *单一职责原则:*你可以将接口或数据转换代码从程序主要业务逻辑中分离。
  • *开闭原则:*只要客户端代码通过客户端接口与适配器进行交互, 你就能在不修改现有客户端代码的情况下在程序中添加新类型的适配器。

2.缺点

  • 代码整体复杂度增加, 因为你需要新增一系列接口和类。 有时直接更改服务类使其与其他代码兼容会更简单。

使用场景

  • 当你希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用适配器类。
  • 如果您需要复用这样一些类, 他们处于同一个继承体系, 并且他们又有了额外的一些共同的方法, 但是这些共同的方法不是所有在这一继承体系中的子类所具有的共性。

参考代码

我们使用一个常见的场景来描述一下:电脑插口

我们的 mac 电脑已经实现了 lighting 接口,但是我们手头上还有一台 Windows 电脑没有实现 lighting 接口,那么我们就需要一个适配器来帮助我们实现了。

go 复制代码
// client.go 客户端代码
package main

import "fmt"

type Client struct{}

func (c *Client) InsertLightningConnectorIntoComputer(com Computer) {
	fmt.Println("Client inserts Lightning connector into computer.")
	com.InsertIntoLightningPort()
}
go 复制代码
// computer.go 客户端接口
package main

type Computer interface {
	InsertIntoLightningPort()
}
go 复制代码
// mac.go 类似服务
package main

import "fmt"

type Mac struct{}

func (m *Mac) InsertIntoLightningPort() {
	fmt.Println("Lightning connector is plugged into mac machine.")
}
go 复制代码
// windows.go 未知服务,外部服务
package main

import "fmt"

type Windows struct{}

func (w *Windows) insertIntoUSBPort() {
	fmt.Println("USB connector is plugged into windows machine.")
}
go 复制代码
// windowsAdapter.go 适配器
package main

import "fmt"

type WindowsAdapter struct {
	windowMachine *Windows
}

func (w *WindowsAdapter) InsertIntoLightningPort() {
	fmt.Println("Adapter converts Lightning signal to USB.")
	w.windowMachine.insertIntoUSBPort()
}
go 复制代码
// main.go
package main

func main() {
	client := &Client{}
	mac := &Mac{}

	client.InsertLightningConnectorIntoComputer(mac)

	windowsMachine := &Windows{}
	windowsMachineAdapter := &WindowsAdapter{
		windowMachine: windowsMachine,
	}

	client.InsertLightningConnectorIntoComputer(windowsMachineAdapter)
}

output:

go 复制代码
Client inserts Lightning connector into computer.
Lightning connector is plugged into mac machine.
Client inserts Lightning connector into computer.
Adapter converts Lightning signal to USB.
USB connector is plugged into windows machine.
相关推荐
CodeCraft Studio4 小时前
PDF处理控件Aspose.PDF教程:使用 Python 将 PDF 转换为 Base64
开发语言·python·pdf·base64·aspose·aspose.pdf
零点零一4 小时前
VS+QT的编程开发工作:关于QT VS tools的使用 qt的官方帮助
开发语言·qt
lingchen19066 小时前
MATLAB的数值计算(三)曲线拟合与插值
开发语言·matlab
gb42152877 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
一朵梨花压海棠go7 小时前
html+js实现表格本地筛选
开发语言·javascript·html·ecmascript
蒋星熠7 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
翻滚丷大头鱼7 小时前
Java 集合Collection—List
java·开发语言
aramae8 小时前
C++ -- 模板
开发语言·c++·笔记·其他
胡耀超8 小时前
4、Python面向对象编程与模块化设计
开发语言·python·ai·大模型·conda·anaconda
索迪迈科技8 小时前
java后端工程师进修ing(研一版 || day40)
java·开发语言·学习·算法