-3

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.

2

1 Answer 1

0

I'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")
}
Sign up to request clarification or add additional context in comments.

1 Comment

I want nothing code in func (p *peopleManager) do() {, only call method of substruct. I know, what an embedded struct don't have an access to the outer struct, I want find Golang way to do something like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.