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?
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: