dougaodi8895 2016-03-02 12:11
浏览 9
已采纳

部分实现接口的最佳实践

What would be the go-way of partly providing a default implementation?

To illustrate, the following simple example of a toggle switch driver is the dead-end I ended with by following my OO intuition... Of course it does not compile (I know why) and I am not necessarily willing to do so. Any other solution better fitting into the go philosophy would be in fact even better to correctly understand the go-way for this common need.


The complete example can also be found at https://play.golang.org/p/MYED1PB-dS

Given the following interface:

type ToggleSwitch interface {
    TurnOn()
    TurnOff()
    IsOn() bool
    Toggle()
}

Toggle() is a good candidate to be provided a default implementation (ie, according to the current state, turn on or off the switch):

// The Toggle() method can already be defined using TurnOn, TurnOff() and IsOn().
type DefaultDriver struct {
}

// The following implementation would be fine for non-optimized cases:
func (d *DefaultDriver) Toggle() {
    state := d.IsOn()
    fmt.Println("generic toogle ->", state)
    if state {
        d.TurnOff()
    } else {
        d.TurnOn()
    }
}

And then an actual driver could use it or not:

// Example of an actual ToggleDriver which should fully implement the interface
// based on the default implementation or not.
// For example, if the toggle switch device has a bult-in toggle command, the
// Toggle() method could be optimized to directly use it.
type DummyDriver struct {
    *DefaultDriver // promote DefaultDriver methods
    state bool
}

func (d *DummyDriver) IsOn() bool {
    return d.state
}

func (d *DummyDriver) TurnOff() {
    d.state = false
}

func (d *DummyDriver) TurnOn() {
    d.state = false
}

// Uncomment me to optimize me...
//func (d *DummyDriver) Toggle() {
//  fmt.Println("specialized toogle ->", d.state)
//  d.state = !d.state
//}
  • 写回答

2条回答 默认 最新

  • doutuan4361 2016-03-02 12:58
    关注

    In my opinion what you're trying to do isn't very Go-like.

    ToggleSwitch defines four methods that clearly work on some state. In order to provide an implementation for any of these methods, you also need to implement that state (even if that state's non-existent, e.g. by defining those methods as no-ops), but at that point, it simply doesn't make sense not to provide all those methods.

    With Go-like type composition the embedded types should generally be "whole", complete and useful on their own. Methods provided by the embedded type only work on that field alone, there's no way to get to the "parent" or its methods.

    If ToggleSwitch also had other methods that didn't deal with that state, then it'd make sense to provide only a partial implementation of the interface, but at that point an even better and much more idiomatic solution would be to define ToggleSwitch as a combination of two separate interfaces.

    In other words, I don't think there's a "Go way" to provide a partial implementation of an interface (that's not composed of several interfaces), because it's idiomatic in Go to define interfaces that are as small as possible.

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

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路