I have factory NewPeopleManager and several types of user.
I have Base struct for common behavior of people
And for type of user Working I need change behavior of DoFirst function.
package main
import "fmt"
type PeopleManager interface {
Do()
}
func NewPeopleManager(typeOfUser string) PeopleManager {
switch typeOfUser {
case "working":
return NewWorking()
// also has many others types of user
default:
panic("Not found type")
}
}
type Base struct {
}
func (b *Base) Do() {
b.DoPrepare()
b.DoFirst()
b.DoSecond()
}
func (b *Base) DoPrepare() {
fmt.Println("Call DoPrepare Base type")
}
func (b *Base) DoFirst() {
fmt.Println("Call DoFirst Base type")
}
func (b *Base) DoSecond() {
fmt.Println("Call DoSecond Base type")
}
type Working struct {
Base
}
func NewWorking() *Working {
return &Working{}
}
func (w *Working) Do() { // Method just call Base method to run a process
w.Base.Do()
}
func (w *Working) DoFirst() { // Rewrite just one method on some big chain of methods
fmt.Println("Call DoFirst Working type")
}
func main() {
m := NewPeopleManager("working")
m.Do()
}
I see response:
Call DoPrepare Base type
Call DoFirst Base type
Call DoSecond Base type
But I want see:
Call DoPrepare Base type
Call DoFirst Working type
Call DoSecond Base type
Please suggest Golang way how I can do not rewrite method Do like this:
func (w *Working) Do() {
w.DoPrepare()
w.DoFirst()
w.DoSecond()
}
Because method Do can have many rows and I do not want rewrite it in every typeOfUser of my Factory when it will be needed.
I read documents and do not found decision.
type Working struct { Base }does not mean what you think it means.