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?