Go中的接口层次结构 [英] Interface hierarchy in Go

查看:183
本文介绍了Go中的接口层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序使用多种类型的模块,但所有不同类型的模块共享某些方法。我正在尝试构建一个可以重用于不同类型模块的通用工厂,但是我错过了接口继承或Go中会调用的其他东西。



这是一个我尽量简化的例子:

有一个通用工厂,使用通用的 Module 接口:

  package main 

var(
modules [] Module


类型模块接口{
RegisterFlagSet()
GetName()(string)


类型工厂结构{
实例[]模块
}

func RegisterModules(modules [] Module){
modules =模块
}

func(f * Factory)registerFlagSets(){
for _,inst:= range f.instances {
inst.RegisterFlagSet()


$ b $ func(f * Factory)GetInstance(seek string)(Module){
for _,inst:= range f.instances {
if(inst.GetName()== seek){
return inst
}
}
panic(can not find module)
}

然后对模块类型 Timer 有一个更具体的实现。我试图重复使用尽可能多的工厂:

  package main 

import(
time


var(
timer_modules = []计时器{
//列出所有计时器模块
}


类型定时器接口{
模块
GetTicker()(* time.Ticker)
}

类型TimerFactory结构{
Factory
}

func NewTimerFactory()TimerFactory {
tfact:= TimerFactory {}
RegisterModules(timer_modules)
return tfact
}

当我尝试构建这个错误时:

  timer_factory.go:25:不能使用timer_modules(类型[] Timer)作为类型[] RegisterModules参数中的模块

我不明白为什么不能使用 type [] Timer 变量作为 type [] Module ,因为接口 Module 的所有方法也在接口定时器,所以它们应该兼容或不兼容?有没有办法使它们兼容?

https://golang.org/doc/faq#convert_slice_of_interface 给出了解释。
其中一个解决方法是实现一个新的注册函数:

$ p $ func RegisterModule(m Module){
modules = append(modules,m)
}

并调用函数的范围只需另外两行:

pre $ func NewTimerFactory()TimerFactory {
tfact:= TimerFactory {}
for _,t:=范围timer_modules {
RegisterModule(t)
}
return tfact
}


I have a program where multiple types of modules are used, but all the different types of modules share certain methods. I'm trying to build a generic factory that can be reused for the different types of modules, but I'm missing something like interface inheritance or however that would be called in Go.

This is an example that I tried to simplify as good as possible:

There is a generic factory which uses a generic Module interface:

package main

var (
  modules []Module
)

type Module interface {
  RegisterFlagSet()
  GetName() (string)
}

type Factory struct {
  instances []Module
}

func RegisterModules(modules []Module) {
  modules = modules
}

func (f *Factory) registerFlagSets() {
  for _,inst := range f.instances {
    inst.RegisterFlagSet()
  }
}

func (f *Factory) GetInstance(seek string)(Module) {
  for _,inst := range f.instances {
    if (inst.GetName() == seek) {
      return inst
    }
  }
  panic("cannot find module")
}

Then there's a more specific implementation for the module type Timer. I'm trying to reuse as much of the factory as possible:

package main

import (
  "time"
)

var (
  timer_modules = []Timer{
    // list all the timer modules here
  }
)

type Timer interface {
  Module
  GetTicker() (*time.Ticker)
}

type TimerFactory struct {
  Factory
}

func NewTimerFactory() TimerFactory {
  tfact := TimerFactory{}
  RegisterModules(timer_modules)
  return tfact
}

When I try to build that I get this error:

timer_factory.go:25: cannot use timer_modules (type []Timer) as type []Module in argument to RegisterModules

I don't understand why a variable of type []Timer cannot be used as type []Module because all the methods of the interface Module are also in the interface Timer, so they should be compatible or not? Is there a way to make them compatible?

解决方案

https://golang.org/doc/faq#convert_slice_of_interface gives explanations. One of the workarounds is to implement a new register function:

func RegisterModule(m Module) {
  modules = append(modules, m)
}

and invoke the function in a range at the cost of only two more lines:

func NewTimerFactory() TimerFactory {
  tfact := TimerFactory{}
  for _, t := range timer_modules {
    RegisterModule(t)
  }
  return tfact
}

这篇关于Go中的接口层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆