码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【设计模式】8、adapter 适配器模式


    文章目录

    • 八、adapter 适配器模式
      • 8.1 convert_lightning_to_usb
        • 8.1.1 client_test.go
        • 8.1.2 client.go
        • 8.1.3 computer.go
        • 8.1.4 adapter.go

    八、adapter 适配器模式

    https://refactoringguru.cn/design-patterns/adapter

    通常用于老旧系统, 或第三方系统, 提供一层适配器或插件, 做协议转换

    PS: 如果开发新系统, 各层之间的解耦, 成为 bridge 桥接模式. 而如果是老系统则称为 adapter 适配器模式. 本质是一样的. 都是通过添加中间层实现的.

    8.1 convert_lightning_to_usb

    https://refactoringguru.cn/design-patterns/adapter/go/example

    08adapter/081convert_lightning_to_usb
    ├── adapter.go
    ├── client.go
    ├── client_test.go
    ├── computer.go
    └── readme.md
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    8.1.1 client_test.go

    package _81convert_lightning_to_usb
    
    import "testing"
    
    /*
    === RUN   TestClient
    insert lighting into computer
    mac: insert into lightning port
    insert lighting into computer
    适配器: 将雷电口转换为USB口
    windows: insert into usb port
    --- PASS: TestClient (0.00s)
    PASS
    */
    func TestClient(t *testing.T) {
    	mac := &mac{}
    	windowsAdapter := &windowsAdapter{windowsComputer: &windows{}}
    
    	c := &client{}
    	c.InsertLightningIntoComputer(mac)
    	c.InsertLightningIntoComputer(windowsAdapter)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    8.1.2 client.go

    package _81convert_lightning_to_usb
    
    import "fmt"
    
    type client struct {
    }
    
    func (c *client) InsertLightningIntoComputer(computer computer) {
    	fmt.Println("insert lighting into computer")
    	computer.InsertIntoLightningPort()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    8.1.3 computer.go

    package _81convert_lightning_to_usb
    
    import "fmt"
    
    type computer interface {
    	InsertIntoLightningPort()
    }
    
    type mac struct{}
    
    func (c *mac) InsertIntoLightningPort() {
    	fmt.Println("mac: insert into lightning port")
    }
    
    type windows struct{}
    
    func (c *windows) InsertIntoUSBPort() {
    	// windows 只支持 usb 口, 不支持雷电口
    	fmt.Println("windows: insert into usb port")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    8.1.4 adapter.go

    package _81convert_lightning_to_usb
    
    import "fmt"
    
    type windowsAdapter struct {
    	windowsComputer *windows
    }
    
    func (a *windowsAdapter) InsertIntoLightningPort() {
    	fmt.Println("适配器: 将雷电口转换为USB口")
    	a.windowsComputer.InsertIntoUSBPort()
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
  • 相关阅读:
    Go包介绍与初始化:搞清Go程序的执行次序
    Sora文生视频模型深度剖析:全网独家指南,洞悉98%关键信息,纯干货
    SE(Squeeze and Excitation)模块的理解以及代码实现
    前端面试的话术集锦第 23 篇博文——高频考点(常考算法题解析)
    Web3:数字化社会的下一步
    查询性能提升3倍!Apache Hudi 查询优化了解下?
    orc文件的读写及整合hive
    1004:字符三角形(信奥赛一本通)
    HCIA-实验命令基础学习:
    【面试指南】AI算法面试
  • 原文地址:https://blog.csdn.net/jiaoyangwm/article/details/138047926
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号