机制和策略

机制和策略

机制:如何做?

策略:做什么,何时做?

type Policy struct {
    mechanisms []Mechanism
}

func (p *Policy) Do() {
    // ...

    for _, item := range p.mechanisms {
        // ...

        item.Before()

        // ...

        item.Run()

        // ...

        item.After()

        // ...
    }

    // ...
}

type Mechanism interface {
    Before()
    Run()
    After()
}

type mechanism struct {}

func (m *mechanism) Before() {}
func (m *mechanism) Run() {}
func (m *mechanism) After() {}