dshtze500055 2019-09-01 18:59
浏览 66
已采纳

同一结构的多个接口

I'm learning Go and wondering in some cases if it is considered good/okay/typical (encouraged?) practice in Golang to create several interface variations for the same struct depending on what the consumer code will be doing with that struct?

I'm questioning this because I have a struct object that is arguably doing too much in my codebase and I want to add some tests and mock only certain usages/consumers of that struct. Say I have,

for a (contrived) example, an Environment struct

// Environment/env.go

package env

type Environment struct {
  sunny bool,
  fullMoon bool,
  temp float64
  // ...
}

func (e *Environment) IsSunny() bool {
  return e.sunny
}

func (e *Environment) IsFullMoon() bool {
  return e.fullMoon
}

func (e *Environment) GetTemp() float64 {
  return e.temp
}

The above struct has properties and methods related to some of the environment conditions (day and night time).

Then there are multiple consumers of this struct, but each interface only cares about a subset of the available methods:

// daytime.go

type DayEnv interface {
  IsSunny() bool
  GetTemp() float64
}

func getDaytime(de DayEnv) {
  sunStatus := getSunStatus(de)
  temp      := getDayTemp(de)

  fmt.Printf("Today is %s and temperature is %s", sunStatus, temp)
}

// func getSunStatus(de DayEnv) string {}
// func getDayTemp(de DayEnv) string {}
// nightTime.go

type NightEnv interface {
  IsFullMoon() bool
  GetTemp() float64
}


func getNighttime(ne NightEnv) {
  moonPhase := getMoonPhase(nightEnv)
  temp      := getNightTemp(nightEnv)

  fmt.Printf("Tonight the moon is %s and temperature is %s", moonPhase, temp)
}

// func getMoonPhase(ne NightEnv) string { }
// func getNightTemp(ne NightEnv) string { }

It seems to me while making a new interface that is only concerned with a subset of the struct methods makes things more flexible, it also feels fairly lazy or wrong to have so much (partial) duplication of interfaces and to sprinkle them as need or wherever they are consumed. I realize this example is a bit contrived, but on a larger scale (like many, many consumers), or perhaps one file has x interfaces for the same struct... is there anything wrong with this approach?

  • 写回答

2条回答 默认 最新

  • ds34222222 2019-09-01 19:06
    关注

    There is nothing wrong with this approach, and Go standard library uses it often. For instance, there are many structs that implement a combination of io.Reader, io.Writer, io.Closer, and io.Seeker. The users of those structs specify what type of interface they need and use that.

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

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line