网站建设资讯

NEWS

网站建设资讯

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

直接上代码:

芦山网站建设公司创新互联建站,芦山网站设计制作,有大型网站制作公司丰富经验。已为芦山1000+提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的芦山做网站的公司定做!

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()
}

分享文章:go语言接口适配,适配器模式
本文网址:http://cdweb.net/article/gdsohp.html