Golang設(shè)計(jì)模式之適配器模式詳細(xì)講解
適配器模式
適配器是一種結(jié)構(gòu)型設(shè)計(jì)模式, 它能使不兼容的對象能夠相互合作。
適配器可擔(dān)任兩個(gè)對象間的封裝器, 它會接收對于一個(gè)對象的調(diào)用, 并將其轉(zhuǎn)換為另一個(gè)對象可識別的格式和接口。
概念示例
這里有一段客戶端代碼, 用于接收一個(gè)對象 (Lightning 接口) 的部分功能, 不過我們還有另一個(gè)名為 adaptee 的對象 (Windows 筆記本), 可通過不同的接口 (USB 接口) 實(shí)現(xiàn)相同的功能
這就是適配器模式發(fā)揮作用的場景。 我們可以創(chuàng)建這樣一個(gè)名為 adapter 的結(jié)構(gòu)體:
遵循符合客戶端期望的相同接口 (Lightning 接口)。
可以適合被適配對象的方式對來自客戶端的請求進(jìn)行 “翻譯”。 適配器能夠接受來自 Lightning 連接器的信息, 并將其轉(zhuǎn)換成 USB 格式的信號, 同時(shí)將信號傳遞給 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go實(shí)現(xiàn)將io.Writer轉(zhuǎn)換成字符串
golang中提供了各種類型之間的轉(zhuǎn)換方法,其中,將其他類型轉(zhuǎn)換為字符串類型是常見的操作,本文主要介紹了Go實(shí)現(xiàn)將io.Writer轉(zhuǎn)換成字符串,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05Win10系統(tǒng)下Golang環(huán)境搭建全過程
在編程語言的選取上,越來越多的人選擇了Golang,下面這篇文章主要給大家介紹了關(guān)于Win10系統(tǒng)下Golang環(huán)境搭建的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01go語言通過odbc操作Access數(shù)據(jù)庫的方法
這篇文章主要介紹了go語言通過odbc操作Access數(shù)據(jù)庫的方法,實(shí)例分析了Go語言通過odbc連接、查詢與關(guān)閉access數(shù)據(jù)庫的技巧,需要的朋友可以參考下2015-03-03golang struct擴(kuò)展函數(shù)參數(shù)命名警告解決方法
今天在使用VSCode編寫golang代碼時(shí),定義一個(gè)struct,擴(kuò)展幾個(gè)方法,需要的朋友可以參考下2017-02-02go打包aar及flutter調(diào)用aar流程詳解
這篇文章主要為大家介紹了go打包aar及flutter調(diào)用aar流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Golang實(shí)現(xiàn)自定義recovery中間件
在?Golang?的?Web?項(xiàng)目中,自定義?recovery?中間件是一種常見的做法,用于捕獲并處理應(yīng)用程序的運(yùn)行時(shí)錯(cuò)誤,下面我們就來看看具體如何實(shí)現(xiàn)吧2023-09-09Go常用標(biāo)準(zhǔn)庫之fmt的簡介與使用詳解
fmt 是 Go 語言中的一個(gè)常用標(biāo)準(zhǔn)庫,它用于格式化輸入和輸出數(shù)據(jù),這篇文章主要為大家介紹了fmt的基本使用,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10