将方法重构为接口的一部分

huangapple go评论65阅读模式
英文:

Refactoring method to be part of an interface

问题

我是一个Python开发者,有时候对Go语言的显式特性感到困惑。
我正在尝试重构一些代码,以便将一个方法从一个结构体移动到一个接口中。
但是这个过程对我来说有些奇怪,我希望确认我没有做错什么。

我有以下的接口、结构体和方法:

type Executing interface {
    Execute()
}

type MyExecuter struct {
     attribut1 string
}

//我想要移动的函数
func (exe1 *MyExecuter) format() string {
    return fmt.Sprintf("formated : %s", exe1.attribut1) 
}


func (exe1 *MyExecuter) Execute() {
    //执行
    fmt.Println(exe.format())
}



func GetExecuter() Executer {
    return MyExecuter{attribut1: "test"}
}

所以这里我有一个通用的Execute接口,这个接口将被GetExecuter方法返回的对象访问。

现在,作为我的一个Executer实现的一部分,我想将Format方法作为一个接口的一部分移动。
所以我做了以下操作:

type Formatting interface {
    format() string
}

type Formatter struct {}

func (formatter *Formatter) format(exe1 *MyExecuter) string {
    return fmt.Sprintf("formated : %s", exe1.attribut1)
}

所以我创建了一个新的接口,一个新的空结构体,并更新了我的函数,使其以我的之前的结构体作为属性。

虽然这似乎可以工作,但我觉得这有点复杂。特别是我需要在方法中添加对我的初始对象的引用的部分。我在这里做错了什么吗,还是这是正确的方式?

英文:

I am an ex python dev sometimes struggling with the explicit nature of Go.
I am trying here to refactor some code in order to be able move a method from one structure to be part of an interface.
But the process seems weird to me, I wish to confirm I am not doing something incorrectly.

I have the following interfaces, structure and methods:

type Executing interface {
    Execute()
}

type MyExecuter struct {
     attribut1 string
}

//The function I wish to move
func (exe1 *MyExecuter) format() string {
    return fmt.sprintf ("formated : %s", exe1.attribut1) 
}


func (exe1 *MyExecuter) Execute() {
    //Executing
    fmt.Println(exe.format())
}



func GetExecuter () Executer{
    return MyExecuter{attribut1: "test"}
}

So here I have a generic interface Execute, this interface will be accessed by the object returned by the GetExecuter method.

Now, as part of the implementation of one of my Executer, I want to move the Format method as part of an interface.
So I am doing the following:

type Formatting interface {
    format() string
}

type Formatter struct {}

func (formatter *Formatter) format(exe1 *MyExecuter) (string) {
    return fmt.sprintf ("formated : %s", exe1.attribut1)
}

So I create a new interface, a new empty structure, and update my function to take as attribute my previous structure.

While this seems to work, it seems to me this is a bit convoluted. Specially the part where I need to add a reference to my initial object as attribute of the method. Am I doing something wrong here, or this is the right way?

答案1

得分: 3

您的Executer实现已经实现了Formatting接口:

type Executing interface {
    Execute()
}

type Formatting interface {
    format() string
}

func (exe1 MyExecuter) format() string {
    return fmt.Sprintf("格式化:%s", exe1.attribut1)
}

func (exe1 MyExecuter) Execute() {
    //执行操作
    fmt.Println(exe.format())
}

v := MyExecuter{}
// 在这里,v实现了Executing和Formatting接口

这里需要注意一点:您的代码显示了指针接收器。这意味着方法是为*MyExecuter定义的,而不是为MyExecuter定义的。因此,您必须将指向结构实例的指针传递给它才能正常工作。或者,如上所示,使用值接收器,以便方法同时为MyExecuter*MyExecuter定义。

英文:

Your Executer implementation already implements the Formatting interface:

type Executing interface {
    Execute()
}

type Formatting interface {
    format() string
}

func (exe1 MyExecuter) format() string {
    return fmt.sprintf ("formated : %s", exe1.attribut1) 
}


func (exe1 MyExecuter) Execute() {
    //Executing
    fmt.Println(exe.format())
}

v:=MyExecuter{}
// Here, v implements Executing and Formatting interfaces

One thing to note here: Your code shows pointer receivers. That means the methods are defined for *MyExecuter, but not for MyExecuter. So you have to pass pointers to the struct instance for this to work. Or, as I did above, use the value receivers so the methods are defined for both MyExecuter and *MyExecuter.

huangapple
  • 本文由 发表于 2021年11月23日 04:42:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/70072141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定