admin管理员组文章数量:1313179
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.
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.
Share Improve this question asked Jan 31 at 4:45 Roman ShuplovRoman Shuplov 31 bronze badge 2 |1 Answer
Reset to default 0I'm not sure what you are trying to achieve but an embedded struct don't have an access to the outer struct. Maybe you can try something like:
package main
import "fmt"
type PeopleManager interface {
Do()
}
type doer interface {
do()
}
type peopleManager struct {
impl doer
}
func NewPeopleManager(typeOfUser string) PeopleManager {
switch typeOfUser {
case "working":
return &peopleManager{newWorking()}
// also has many others types of user
default:
panic("Not found type")
}
}
func (p *peopleManager) do() {
p.doPrepare()
p.impl.do()
p.doFinish()
}
func (p *peopleManager) doPrepare() {
fmt.Println("Call doPrepare peopleManager type")
}
func (p *peopleManager) doFinish() {
fmt.Println("Call doFinish peopleManager type")
}
type working struct {
peopleManager
}
func newWorking() *working {
return &working{}
}
func (w *working) do() {
fmt.Println("working do")
}
本文标签: goGolang way instead of inheritance using factory patternStack Overflow
版权声明:本文标题:go - Golang way instead of inheritance using factory pattern? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741926040a2405291.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
type Working struct { Base }
does not mean what you think it means. – Dai Commented Jan 31 at 4:55