温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

go语言接口适配,适配器模式

发布时间:2020-07-21 09:53:08 来源:网络 阅读:2844 作者:fangnux 栏目:编程语言

直接上代码:

package main                                                                   
                                                                               
import "log"                                                                   
                                                                               
type LogIN interface {                                                         
    WARN(string)                                                               
    INFO(string)                                                               
    //其他省略                                                                 
}                                                                              
                                                                               
/*                                                                             
假设你写了一个框架,框架里需要打日志,具体是个什么样的日志由使用者决定          
*/                                                                             
type Frame struct {                                                            
    name string                                                                
    log  LogIN                                                                 
}                                                                              
                                                                               
func (f *Frame) Start() {                                                      
    f.log.INFO("frame start...")                                               
}                                                                              
                                                                               
//但是问题来了,使用者用了一个这样的日志库                                     
                                                                               
type RealLog struct {                                                          
}                                                                              
                                                                               
func (l *RealLog) warn(s string) {                                             
    log.Print(s)                                                               
}                                                                              
                                                                               
func (l *RealLog) info(s string) {                                             
    log.Print(s)                                                               
}                                                                              
                                                                               
//可以看到,真正使用的日志与框架定义的日志接口不一样,如此我们就需要适配       
                                                                               
type AdaptLog struct {                                                         
    rellog *RealLog                                                            
}                                                                              
                                                          
func (alog *AdaptLog) WARN(s string) {                                         
    alog.rellog.warn(s)                                                        
}

func (alog *AdaptLog) INFO(s string) {
    alog.rellog.info(s)
}

//有了这个适配器,我们就可以很好的在框架里使用RealLog了

func main() {
    adaptlog := AdaptLog{&RealLog{}}
    frame := Frame{name: "frame", log: &adaptlog}

    frame.Start()
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI