Golang設(shè)計(jì)模式之適配器模式詳細(xì)講解
適配器模式
適配器是一種結(jié)構(gòu)型設(shè)計(jì)模式, 它能使不兼容的對(duì)象能夠相互合作。
適配器可擔(dān)任兩個(gè)對(duì)象間的封裝器, 它會(huì)接收對(duì)于一個(gè)對(duì)象的調(diào)用, 并將其轉(zhuǎn)換為另一個(gè)對(duì)象可識(shí)別的格式和接口。

概念示例
這里有一段客戶端代碼, 用于接收一個(gè)對(duì)象 (Lightning 接口) 的部分功能, 不過我們還有另一個(gè)名為 adaptee 的對(duì)象 (Windows 筆記本), 可通過不同的接口 (USB 接口) 實(shí)現(xiàn)相同的功能
這就是適配器模式發(fā)揮作用的場(chǎng)景。 我們可以創(chuàng)建這樣一個(gè)名為 adapter 的結(jié)構(gòu)體:
遵循符合客戶端期望的相同接口 (Lightning 接口)。
可以適合被適配對(duì)象的方式對(duì)來自客戶端的請(qǐng)求進(jìn)行 “翻譯”。 適配器能夠接受來自 Lightning 連接器的信息, 并將其轉(zhuǎn)換成 USB 格式的信號(hào), 同時(shí)將信號(hào)傳遞給 Windows 筆記本的 USB 接口。
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()
}
computer.go: 客戶端接口
package main
type Computer interface {
InsertIntoLightningPort()
}
mac.go: 服務(wù)
package main
import "fmt"
type Mac struct {
}
func (m *Mac) InsertIntoLightningPort() {
fmt.Println("Lightning connector is plugged into mac machine.")
}
windows.go: 未知服務(wù)
package main
import "fmt"
type Windows struct{}
func (w *Windows) insertIntoUSBPort() {
fmt.Println("USB connector is plugged into windows machine.")
}
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()
}
main.go
package main
func main() {
client := &Client{}
mac := &Mac{}
client.InsertLightningConnectorIntoComputer(mac)
windowsMachine := &Windows{}
windowsMachineAdapter := &WindowsAdapter{
windowMachine: windowsMachine,
}
client.InsertLightningConnectorIntoComputer(windowsMachineAdapter)
}
output.txt: 執(zhí)行結(jié)果
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.
到此這篇關(guān)于Golang設(shè)計(jì)模式之適配器模式詳細(xì)講解的文章就介紹到這了,更多相關(guān)Go適配器模式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語言kube-scheduler深度剖析與開發(fā)之pod調(diào)度
這篇文章主要為大家介紹了Go語言kube-scheduler深度剖析與開發(fā),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
GoLand編譯帶有構(gòu)建標(biāo)簽的程序思路詳解
這篇文章主要介紹了GoLand編譯帶有構(gòu)建標(biāo)簽的程序,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
關(guān)于golang高并發(fā)的實(shí)現(xiàn)與注意事項(xiàng)說明
這篇文章主要介紹了關(guān)于golang高并發(fā)的實(shí)現(xiàn)與注意事項(xiàng)說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-05-05
Go語言Gin框架中使用MySQL數(shù)據(jù)庫的三種方式
本文主要介紹了Go語言Gin框架中使用MySQL數(shù)據(jù)庫的三種方式,通過三種方式實(shí)現(xiàn)增刪改查的操作,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
golang利用unsafe操作未導(dǎo)出變量-Pointer使用詳解
這篇文章主要給大家介紹了關(guān)于golang利用unsafe操作未導(dǎo)出變量-Pointer使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

