dongye1143 2017-07-30 03:09
浏览 37
已采纳

使用多种方法进行接口-可接受还是不可接受?

Is there anything wrong with an interface with more that one function assigned to it ?

Everywhere I read, an interface should ideally have only one method (which is what the interface should be named after). But are there any pit falls to having more than one method for an interface ? Ex.

type FooMgrInterface interface {
    CreateFoo(hostname string, fooConfig interface{}) (uuid string, err error)
    DeleteFoo(hostname string, fooID string) (err error)
    CreateBar(hostname string, barID string, barConfig interface{}) (uuid string, err error)
    DeleteBar(hostname string, barID string) (err error)
    AttachBar(hostname string, fooID string, bars []string) (err error)
    DetachBar(hostname string, barID string) (err error)
    GetBars(hostname string) (bars []Bar, err error)
    GetBar(hostname string, barID string) (bar Bar, err error)
    GetFoo(hostname string, fooID string) (foo Foo, err error)
    GetFoos(hostname string) (foos []Foo, err error)
}

If so how could the above interface be simplified or (maybe) split into multiple interfaces ?

  • 写回答

2条回答 默认 最新

  • duanfei8897 2017-07-30 14:24
    关注

    Look for inspiration in https://golang.org/src/io/io.go

    You will see:

    a. "Atomic" interfaces: Reader, Writer, Closer, Seeker

    b. Composed interfaces: ReaderWriter, ReaderWriterSeeker, ReaderSeekerCloser etc.

    Golang will not complaint about gigantic interfaces, it's you and your collegues will have complaints about big monolithic interfaces.

    I recomend divide your interface to 4 (maybe 2) interfaces: FooOps, FoosOps, BarOps, BarsOps and then define composed interfaces from them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 github符合条件20分钟秒到账,github空投 提供github账号可兑换💰感兴趣的可以找我交流一下
  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?