英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论