mardi 12 octobre 2021

Is there a proper Design Pattern for settings or options of multiple layers?

I'm designing a web framework,which supports custom options(e.g. timeout) for each request from the function call if provided; otherwise, from the client instance; otherwise, from database/config files/source codes.

type Option func( /* receiver */ )

func WithTimeout(sec int) Option {
    return func( /* receiver */ ) { /* set timeout */ }
}

type Client struct{ Options []Option }

func NewClient(options ...Option) *Client {
    return &Client{Options: options}
}

func (c *Client) Send(options ...Option) {
    // how to implement it?
    // arguments -> instance -> cmd flags -> config file -> source code...
}

var DefaultOptions = []Option{WithTimeout(10)}

func main() {
    c := NewClient(WithTimeout(5))
    c.Send(WithTimeout(3))
}

So what is the proper design pattern?

Aucun commentaire:

Enregistrer un commentaire