donglianjiang9321 2019-09-15 05:48 采纳率: 0%
浏览 57

通用方法和建议如何消除依赖

I have a question regarding dependency injection. Please consider the example below.

For example, selector() is a function that select something and guarantee return an interface

In this example

bar.node.go

type NodeTemplate struct {
     Name string
}

// satisfy interface declared in db.foo.go 
//but never imports anything from db.foo.go
func (node *NodeTemplate) GetUuidName() string {
   if node != nil {
      return node.Name
   }
   return 
}

db.foo.go

// interface declared in db.foo.go

type Node interface {
   GetUuidName() string
}

Option A // So selector receives a map of Some interface and populate a map

func SelectSomething(nodemap map[string]Node, selectFactor string) {
     // selection from db and result populate in a map
}

Option B

Another pattern SelectSomething return a Node and it Interface

So another package will depend on importing Node and that will introduce a dependency.

func SelectSomething(seleconsomething) []*Node {
  // do selection and return a slice of SomeInterface

n := &Node{} // here it need to be concret type T
return Node
}

So based on logic I've described I see the first approach is better but in that approach, select need do concrete type allocation in order to populate a map.

Consider another example

db.foo.go

type Node interface {
   GetUuidName() string
}

func inserter(node *Node) error {
   // do some work 
   node.GetUuidName()
}

For a case like in inserter case, inserter has no external dependency, inserter just needs to receive something that satisfies the interface. Declare interfaces locally and that brake a dependancy.

But in the case of selector example, it has to do memory allocation in order to return or populate a map or return something that has concrete type T. So in both case, it has to have internal re-presentation.

So here is my question can selector somehow at run time figure out a type it receives based on the interface and instantiate an object of that type and insert to a map as an interface or return a slice of the interface. ?

By doing so selector function will have no dependancy on what it receives it just guarantee it will instantiate the same object type T and return interface.

or can selector return interface but I guess I have to have a bi-directional interface between db package and package X or dynamic dispatcher need to do some magic ?

  • 写回答

1条回答 默认 最新

  • duanjiao3686 2019-09-15 07:35
    关注

    You want a type to behave in a certain way. That is achieved via an interface. This case is no different. Simply add the desired behavior to your interface, as demonstrated below with the Foo interface.

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    type Foo interface {
        Bar()
        TypeOf() reflect.Type
    }
    
    type Baz struct{}
    
    func (b Baz) Bar() {
        fmt.Println("I am a Fooer!")
    }
    
    func (b Baz) TypeOf() reflect.Type {
        return reflect.TypeOf(b)
    }
    
    func DoSomeThing(f Foo) {
        f.Bar()
        fmt.Println(f.TypeOf())
    }
    
    func main() {
        fmt.Println("Hello, playground")
        b := Baz{}
        DoSomeThing(b)
    }
    

    <kbd>Run on playground</kbd>

    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝