Hello!
Another way to do this is: play.golang.org/p/tmzumHEIo2H
Which does not use embedding or types implementing all the methods but rather functionals options and a default implementation; with the big caveat the concrete implementations are not sharing state in their methods:
package main import "fmt" func main() { bef := BeforeSensor{} afs := AfterShutdownSensor{} s := NewSensor(WithBeforeSensor(&bef), WithAfterSensor(&afs), WithShutdownSensor(&afs)) s.Process() } type ( BeforeStarter interface { BeforeStart() } AfterStarter interface { AfterStart() } Shutdowner interface { Shutdown() } Processer interface { Process() } ) type Sensor struct { before BeforeStarter after AfterStarter shutdown Shutdowner process Processer } type SensorOption func(s *Sensor) func WithBeforeSensor(b BeforeStarter) SensorOption { return func(se *Sensor) { se.before = b } } func WithAfterSensor(a AfterStarter) SensorOption { return func(se *Sensor) { se.after = a } } func WithShutdownSensor(s Shutdowner) SensorOption { return func(se *Sensor) { se.shutdown = s } } func WithProcessSensor(p Processer) SensorOption { return func(se *Sensor) { se.process = p } } func NewSensor(opts ...SensorOption) *Sensor { s := Sensor{ before: &DefaultSensor{}, after: &DefaultSensor{}, process: &DefaultSensor{}, shutdown: &DefaultSensor{}, } for _, opt := range opts { opt(&s) } return &s } func (s *Sensor) Process() { s.before.BeforeStart() s.after.AfterStart() s.process.Process() s.shutdown.Shutdown() } //- type DefaultSensor struct{} func (d DefaultSensor) BeforeStart() { fmt.Println("DefaultSensor BeforeStart") } func (d DefaultSensor) AfterStart() { fmt.Println("DefaultSensor AfterStart") } func (d DefaultSensor) Shutdown() { fmt.Println("DefaultSensor Shutdown") } func (d DefaultSensor) Process() { fmt.Println("DefaultSensor Process") } //- BeforeSensor type BeforeSensor struct{} func (s BeforeSensor) BeforeStart() { fmt.Println("OnlyBeforeSensor BeforeStart") } //- AfterShutdownSensor type AfterShutdownSensor struct{} func (s AfterShutdownSensor) AfterStart() { fmt.Println("AfterShutdownSensor AfterStart") } func (s AfterShutdownSensor) Shutdown() { fmt.Println("AfterShutdownSensor Shutdown") }
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hello!
Another way to do this is: play.golang.org/p/tmzumHEIo2H
Which does not use embedding or types implementing all the methods but rather functionals options and a default implementation; with the big caveat the concrete implementations are not sharing state in their methods: