适配器模式
被适配目标接口
package adapter
type source interface {
SpecificRequest() string
}
被适配的目标
package adapter
type sourceImpl struct {}
func NewSourceImpl() source {
return &sourceImpl{}
}
func (a *sourceImpl)SpecificRequest() string {
return "adapter message"
}
适配的目标接口
package adapter
type target interface {
Request() string
}
适配的目标
package adapter
type targetImpl struct {
source
}
func NewTargetImpl(a source) target {
return &targetImpl{source:a}
}
func (t *targetImpl)Request() string {
return t.source.SpecificRequest()
}
使用者
package adapter
type User struct{
tg target
}
func NewUser(tg target) *User {
return &User{tg:tg}
}
func (u *User)getResult() {
fmt.Println(u.tg.Request())
}
测试文件
package adapter
func TestAdapter(t *testing.T) {
a := NewSourceImpl()
tg := NewTargetImpl(a)
u := NewUser(tg)
u.getResult()
}
标签:return,package,适配器,模式,source,func,tg,adapter
From: https://www.cnblogs.com/mathsmouse/p/16741996.html