91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

江孜县| 宜黄县| 长寿区| 南昌县| 江西省| 绥中县| 黎平县| 枝江市| 山丹县| 子长县| 集安市| 余庆县| 许昌县| 时尚| 沅江市| 应用必备| 公安县| 响水县| 获嘉县| 兴义市| 兴宁市| 郁南县| 工布江达县| 高淳县| 五河县| 牡丹江市| 外汇| 平遥县| 措美县| 澄迈县| 泸州市| 金华市| 华亭县| 三都| 平阴县| 博罗县| 神池县| 吉隆县| 伊金霍洛旗| 宣武区| 莒南县|