[06]Go设计模式:适配器模式(Adapter Pattern)
适配器模式
一、简介
适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。例如: 在现实生活中,我们的笔记本电脑的工作电压大多数都是20V,而我国的家庭用电是220V,如何让20V的笔记本电脑能够工作在220V的电压下工作?答案:引入一个电源适配器,俗称变压器,有了这个电源适配器,生活用电和笔记本电脑即可兼容。
二、代码
package main
import "fmt"
// 将一个类的接口转换成客户希望的另一个接口,Adapter模式使得原本由于接口
// 不兼容而不能一起工作的那些类可以一起工作
// 定义一个新的接口
type Target interface {
Process()
}
// 定义一个老的接口
type Adaptee interface {
Foo()
Bar()
}
// 定一个Adapter接口
type Adapter struct {
adaptee Adaptee
}
func (a *Adapter) Process(){
fmt.Println("在Adapter中执行process()")
a.adaptee.Bar()
a.adaptee.Foo()
}
// 实现旧接口struct
type oldAdaptee struct {
}
func (o *oldAdaptee) Foo(){
fmt.Println("在旧接口中执行foo()")
}
func (o *oldAdaptee) Bar(){
fmt.Println("在旧的接口中执行bar()")
}
func main(){
oa := new(oldAdaptee)
// 定一个适配器对象
adapter := new(Adapter)
adapter.adaptee = oa
adapter.Process()
}
完整代码地址: https://gitee.com/ncuzhangben/GoStudy/tree/master/go-design-pattern/06-Adapter
三、参考资料
1、 https://www.runoob.com/design-pattern/adapter-pattern.html
2、 https://blog.csdn.net/cloudUncle/article/details/83448727
[06]Go设计模式:适配器模式(Adapter Pattern)的更多相关文章
- 设计模式 - 适配器模式(adapter pattern) 具体解释
适配器模式(adapter pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 适配器模式(adapter pattern): 将一个类的接 ...
- 设计模式 - 适配器模式(adapter pattern) 枚举器和迭代器 具体解释
适配器模式(adapter pattern) 枚举器和迭代器 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考适配器模式(adapter patter ...
- C#设计模式——适配器模式(Adapter Pattern)
一.概述在软件开发中,常常会想要复用一个已经存在的组件,但该组件的接口却与我们的需要不相符,这时我们可以创建一个适配器,在需复用的组件的接口和我们需要的接口间进行转换,从而能够正常的使用需复用的组件. ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- 怎样让孩子爱上设计模式 —— 7.适配器模式(Adapter Pattern)
怎样让孩子爱上设计模式 -- 7.适配器模式(Adapter Pattern) 标签: 设计模式初涉 概念相关 定义: 适配器模式把一个类的接口变换成client所期待的还有一种接口,从而 使原本因接 ...
- 设计模式系列之适配器模式(Adapter Pattern)——不兼容结构的协调
模式概述 模式定义 模式结构图 模式伪代码 类适配器,双向适配器,缺省适配器 类适配器 双向适配器 缺省适配器 模式应用 模式在JDK中的应用 模式在开源项目中的应用 模式总结 主要优点 主要缺点 适 ...
- 二十四种设计模式:适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern) 介绍将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作.示例有一个Message实体类 ...
- 【设计模式】适配器模式 Adapter Pattern
适配器模式在软件开发界使用及其广泛,在工业界,现实中也是屡见不鲜.比如手机充电器,笔记本充电器,广播接收器,电视接收器等等.都是适配器. 适配器主要作用是让本来不兼容的两个事物兼容和谐的一起工作.比如 ...
- 设计模式(七): 通过转接头来观察"适配器模式"(Adapter Pattern)
在前面一篇博客中介绍了“命令模式”(Command Pattern),今天博客的主题是“适配器模式”(Adapter Pattern).适配器模式用处还是比较多的,如果你对“适配器模式”理解呢,那么自 ...
随机推荐
- Pycharm中打开Terminal方式
点击剪头的图标就可以在左侧出现Terminal
- vue组件通信方式(多种方案)
一.Props传递数据 components |-Grandson1.vue //孙子1 |-Grandson2.vue //孙子2 |-Parent.vue //父亲 |-Grandson1.vue ...
- makefile的三个变量
“$@”代表目标文件. “$^”代表所有的依赖文件. “$<”代表第一个依赖文件. main:main.o mytool1.o mytool2.o gcc -o $@ $^ .c.o: gcc ...
- 定时任务 Scheduled quartz
在项目应用中往往会用到任务定时器的功能,比如某某时间,或者多少多少秒然后执行某个骚操作等.spring 支持多种定时任务的实现,其中不乏自身提供的定时器.接下来介绍一下使用 spring 的定时器和使 ...
- JS 不声明第三个变量的情况下实现两数变换
1. var a = 1; var b = 2; a = a + b; b = a - b; a = a - b; console.log(a); console.log(b); 2. var a = ...
- ssh:no matching host key type found. Their offer: ssh-dss
最近突然ssh 服务连接出现 no matching host key type found. Their offer: ssh-dss 以前一直没有问题 可能的原因 openssh 服务升级,加密算 ...
- Automatic Annotation of Airborne Images by Label Propagation Based on a Bayesian-CRF Model
贝叶斯+全连接条件场,无人机和航片数据,通过标注航片数据自动生成无人机标注数据,具体不懂
- 【AtCoder】 ARC 102
link C-Triangular Relationship 发现要么全部是\(K\)的倍数,要么全部是模\(K\)余\(K/2,(K=2n)\) #include<bits/stdc++.h& ...
- kvm错误:failed to initialize KVM: Permission denied
错误1: 启动kvm容器报错: # virsh start hadoop-test error: Failed to start domain hadoop-testerror: internal e ...
- dedecms 模板文件不存在,无法解析文档的终极各种解决办法
dedecms 模板文件不存在,无法解析文档"的终极各种解决办法 方法一:[此对应喜欢把模板文件使用".html"的格式,] /include/arc.archives. ...