中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

泰顺县| 铜川市| 大田县| 微博| 区。| 苗栗县| 新河县| 苏州市| 乌兰察布市| 和静县| 宁武县| 尼木县| 揭阳市| 富蕴县| 偃师市| 楚雄市| 万全县| 新密市| 青铜峡市| 洛宁县| 海南省| 建平县| 清远市| 华坪县| 剑川县| 蒙自县| 日喀则市| 绥阳县| 广安市| 巴青县| 东安县| 南昌县| 甘南县| 沁水县| 沙雅县| 辰溪县| 穆棱市| 同德县| 鄯善县| 横峰县| 财经|