dtzd65908 2018-02-22 16:36
浏览 77
已采纳

在Go中包装多个实现

I have an application that has multiple concurrent implementations of the same API (e.g. one backed by a SQL database and another by a dataset stored in an XML file). What I'd really like to do is to define a parent type for each type of thing in the API that

  1. holds the member variables that are common to all of the implementations and

  2. defines the methods that all implementations must have.

So, in (invalid) Go, I want to do something like:

type Person interface {
    Name string
    Title string
    Position string
    Boss() *Person
}

type Person_XML struct {
    Person
}

func (p *Person_XML) Boss() (*Person, error) {
    // Poke around an XML document and answer the question
    return boss, nil
}

type Person_SQL {
    Person
}

func (p *Person_SQL) Boss() (*Person, error) {
    // Do a DB query and construct the record for the boss
    return boss, nil
}

But, of course, that's not legal since only structs have member variables and only interfaces have member functions. I could do this with just interfaces like this:

type Person interface {
    Name() string
    Title() string
    Position() string
    Boss() Person
}

type Person_XML struct {
    NameValue string
    TitleValue string
    PositionValue string
    Person
}

func (p *Person_XML) Name() string {
    return p.NameValue
}

func (p *Person_XML) Title() string {
    return p.TitleValue
}

func (p *Person_XML) Position() string {
    return p.PositionValue
}

func (p *Person_XML) Boss() (Person, error) {
    // Poke around an XML document and answer the question
    return boss, nil
}

and similarly for other implementations. Is there an alternative that doesn't force me to turn member variables into member functions? What's best practice for such a use case?

  • 写回答

2条回答 默认 最新

  • duan7007 2018-02-22 17:08
    关注

    Best practice would be to provide an interface:

    type Person interface {
        PersonName() string
        PersonTitle() string
        PersonPosition() string
        Boss() (Person, error)
    }
    

    And also provide a struct which contains the common fields and the methods to get them:

    type BasePerson struct {
        Name     string
        Title    string
        Position string
    }
    
    func (p *BasePerson) PersonName() string     { return p.Name }
    func (p *BasePerson) PersonTitle() string    { return p.Title }
    func (p *BasePerson) PersonPosition() string { return p.Position }
    

    (Note: *BasePerson itself does not implement Person as it doesn't have a Boss() method.)

    Any type that embeds *BasePerson will automatically have its methods promoted, and so to implement Person, only the Boss() method will need to be added.

    For example:

    type PersonXML struct {
        *BasePerson
    }
    
    func (p *PersonXML) Boss() (Person, error) {
        // Poke around an XML document and answer the question
        var boss *PersonXML
        return boss, nil
    }
    

    *PersonXML does implement Person.

    Example using it:

    var p Person
    p = &PersonXML{
        BasePerson: &BasePerson{
            Name:     "Bob",
            Title:    "sysadmin",
            Position: "leader",
        },
    }
    fmt.Println(p.PersonName())
    

    Output (try it on the Go Playground):

    Bob
    

    To create the PersonSQL type, you again only have to add the Boss() method if you embed *BasePerson:

    type PersonSQL struct {
        *BasePerson
    }
    
    func (p *PersonSQL) Boss() (Person, error) {
        // Do a DB query and construct the record for the boss
        var boss *PersonSQL
        return boss, nil
    }
    

    *PersonSQL again does implement Person.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度