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://bzwzjz.com/article/gdsohp.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 响应式网站设计方案 成都网站设计公司 网站制作 成都商城网站建设 成都品牌网站设计 LED网站设计方案 定制网站制作 成都营销网站建设 重庆网站制作 广安网站设计 重庆电商网站建设 成都网站建设 网站制作 成都网站制作 成都网站制作 网站建设推广 成都网站建设 成都网站设计 高端网站建设 企业网站设计 上市集团网站建设 网站建设