package main
import (
“fmt”
)
type IComponent interface {
GetParent() IComponent
SetParent(parent IComponent)
Tick()
}
type IUpdatable interface {
Update()
}
type Component struct {
Name string
Parent IComponent
}
func (c *Component) GetParent() IComponent {
return c.Parent
}
func (c *Component) SetParent(parent IComponent) {
c.Parent = parent
}
func (c *Component) AddChild(child IComponent) {
child.SetParent(c)
//other
}
func (c *Component) Tick() {
fmt.Println(“base Tick”)
}
type ComponentA struct {
Component
}
type ComponentB struct {
Component
}
func (c *ComponentB) Update() {
fmt.Println(“ComponentB Update”)
}
func (c *ComponentB) Tick() {
fmt.Println(“ComponentB Tick”)
c.Update()
}
func init() {
}
func RunComponent1() {
fmt.Println(“#####################RunComponent1##################################”)
child := new(ComponentA)
child.Name = “child”
parent := new(ComponentB)
parent.Name = "parent"
parent.AddChild(child)
parent.Tick()
var updater1 IUpdatable = parent
updater1.Update()
a1Parent := child.GetParent()
if a1Parent == parent {
fmt.Println("==")
}
a1Parent.Tick()
updater2, ok := a1Parent.(IUpdatable)
if ok {
updater2.Update()
}
}
func RunComponent2() {
fmt.Println(“#####################RunComponent2##################################”)
child := new(ComponentA)
child.Name = “child”
parent := new(ComponentB)
parent.Name = "parent"
parent.Tick()
child.SetParent(parent)
var updater1 IUpdatable = parent
updater1.Update()
a1Parent := child.GetParent()
if a1Parent == parent {
fmt.Println("==")
}
a1Parent.Tick()
updater2, ok := a1Parent.(IUpdatable)
if ok {
updater2.Update()
}
}
func main() {
RunComponent1()
RunComponent2()
}