在组合模式中,可以让组件之间进行交互吗?

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

Can go object components talk to each others in composite pattern?

问题

我正在尝试实现组合设计模式。我已经理解了如何组合一个对象的对象。在这个例子中,我有一个运动员和一个游泳函数。

type Athlete struct {
    name string
}

type CompositeAthlete struct {
    athlete Athlete
    Train   func(name string)
}

但是,如果我需要在组合对象创建之后传递名称:

comp := CompositeAthlete{
    athlete: athlete,
    Train:   Swim,
}
comp.Train(athlete.name)

是否可以注入一个能够在注入的对象内部读取的方法?

package main

import (
    "fmt"
    "strings"
)

type Athlete struct {
    name string
}

type CompositeAthlete struct {
    athlete Athlete
    Train   func(name string)
}

func (a *Athlete) Train() {
    fmt.Println("training ...")
}

func Swim(name string) {
    fmt.Println(strings.Join([]string{
        name,
        " is swimming",
    }, ""))
}

func main() {
    fmt.Println("vim-go")

    athlete := Athlete{"Mariottide"}
    athlete.Train()

    comp := CompositeAthlete{
        athlete: athlete,
        Train:   Swim,
    }
    comp.Train(athlete.name)
}

我希望作为组合对象的comp不应该从外部接收名称,而是从运动员那里获取。这可能吗?

英文:

I am trying to implement composite design pattern. I understood how to compose an object of object. In this example I have an athlete and the swim function.

type Athlete struct {
	name string
}

type CompositeAthlete struct {
	athlete Athlete
	Train   func(name string)
}

But if I need to pass the name after composed object creation:

	comp := CompositeAthlete{
		athlete: athlete,
		Train:   Swim,
	}
	comp.Train(athlete.name)

Is it possible to inject a method that is able to read inside the object where is injected;

package main

import (
	"fmt"
	"strings"
)

type Athlete struct {
	name string
}

type CompositeAthlete struct {
	athlete Athlete
	Train   func(name string)
}

func (a *Athlete) Train() {
	fmt.Println("training ...")
}

func Swim(name string) {
	fmt.Println(strings.Join([]string{
		name,
		" is swimming",
	}, ""))
}

func main() {
	fmt.Println("vim-go")

	athlete := Athlete{"Mariottide"}
	athlete.Train()

	comp := CompositeAthlete{
		athlete: athlete,
		Train:   Swim,
	}
	comp.Train(athlete.name)
}

I would like that comp as composed object should not receive name from outside, but from athlete. IS it possible?

答案1

得分: 1

是的,这是可能的。

你可以为CompositeAthlete声明一个Train()方法,该方法将可以访问所有CompositeAthlete的字段(函数和athlete)。

然后你可以在方法内部使用该函数。

以下是更清晰地实现它的方式。

CompositeAthlete定义

(注意,我已将字段更改为TrainFunc,以避免与方法名冲突)

type CompositeAthlete struct {
    athlete Athlete
    TrainFunc   func(name string)
}

然后Train()方法只需执行以下操作:

func (c *CompositeAthlete) Train() {
    c.TrainFunc(c.athlete.name)
}

你可以几乎与之前一样使用它(只是字段名已更改):

comp := CompositeAthlete{
    athlete: athlete,
    TrainFunc:   Swim,
}
comp.Train()

在这个playground中可以看到它的工作原理:

https://play.golang.org/p/xibH_tFers

英文:

Yes, it is possible.

You can declare a Train() method for CompositeAthlete, and that method would have access to all CompositeAthlete fields (the function and the athlete).

Then you can use the function from inside the method.

Here's how you'd implement it, to make it more clear.

CompositeAthlete definition

(note that I have changed the field to TrainFunc so that it does not conflict with the method name)

type CompositeAthlete struct {
    athlete Athlete
    TrainFunc   func(name string)
}

Then the Train() method would just do:

func (c *CompositeAthlete) Train() {
    c.TrainFunc(c.athlete.name)
}

And you would use it almost the same as before (only the field name has changed):

comp := CompositeAthlete{
    athlete: athlete,
    TrainFunc:   Swim,
}
comp.Train()

See it working in this playground:

https://play.golang.org/p/xibH_tFers

huangapple
  • 本文由 发表于 2017年9月17日 23:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/46265534.html
匿名

发表评论

匿名网友

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

确定