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
  • 2 type Working struct { Base } does not mean what you think it means. – Dai Commented Jan 31 at 4:55
  • so, what it means? – Roman Shuplov Commented Feb 5 at 5:46
Add a comment  | 

1 Answer 1

Reset to default 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")
}

本文标签: goGolang way instead of inheritance using factory patternStack Overflow