亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

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

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

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

西和县| 枞阳县| 启东市| 盘锦市| 宜州市| 沈丘县| 达拉特旗| 黄冈市| 平顺县| 万年县| 蒙自县| 虎林市| 静宁县| 宁晋县| 黔西县| 阳谷县| 五大连池市| 敦煌市| 青州市| 河津市| 北川| 子洲县| 南昌县| 花莲县| 南郑县| 邯郸市| 辰溪县| 乌拉特前旗| 海城市| 无极县| 嫩江县| 东莞市| 綦江县| 连南| 乌鲁木齐县| 肃北| 广河县| 北碚区| 禹城市| 静宁县| 五指山市|