選項模式(option)
- Option模式的專業術語為:Functional Options Pattern(函式式選項模式)
- Option模式為golang的開發者提供了將一個函式的引數設定為可選的功能,也就是說我們可以選擇引數中的某幾個,并且可以按任意順序傳入引數,
- 比如針對特殊場景需要不同引數的情況,C++可以直接用多載來寫出任意個同名函式,在任意場景呼叫的時候使用同一個函式名即可;但同樣情況下,在golang中我們就必須在不同的場景使用不同的函式,并且引數傳遞方式可能不同的人寫出來是不同的樣子,這將導致代碼可讀性差,維護性差,
Option模式的優缺點
優點
- 支持傳遞多個引數,并且在引數個數、型別發生變化時保持兼容性
- 任意順序傳遞引數
- 支持默認值
- 方便拓展
缺點
- 增加許多function,成本增大
- 引數不太復雜時,盡量少用
代碼
package main
//options_ok
import (
"fmt"
)
/**
Option模式的優缺點
優點
支持傳遞多個引數,并且在引數個數、型別發生變化時保持兼容性
任意順序傳遞引數
支持默認值
方便拓展
缺點
增加許多function,成本增大
引數不太復雜時,盡量少用
1. 需要Option選項得物件,里面是每一個配置
2. 需要一個Option得介面,擁有一個設定option得函式
3. 需要一個自定義型別得func,實作了Option介面
4. func要回傳一個Option型別得介面,并且引數為要設定得內容
*/
//使用場景,大量修改一個結構體,增加欄位值,初始化極其麻煩
//引入option模式后,直接加一個屬性和一個函式就可以搞定
type Message struct {
id int
name string
address string
phone int
}
func (msg Message) String() {
fmt.Printf("ID:%d \n- Name:%s \n- Address:%s \n- phone:%d\n", msg.id, msg.name, msg.address, msg.phone)
}
func New(id, phone int, name, addr string) Message {
return Message{
id: id,
name: name,
address: addr,
phone: phone,
}
}
type Option func(msg *Message)
var DEFAULT_MESSAGE = Message{id: -1, name: "-1", address: "-1", phone: -1}
func WithID(id int) Option {
return func(m *Message) {
m.id = id
}
}
func WithName(name string) Option {
return func(m *Message) {
m.name = name
}
}
func WithAddress(addr string) Option {
return func(m *Message) {
m.address = addr
}
}
func WithPhone(phone int) Option {
return func(m *Message) {
m.phone = phone
}
}
func NewByOption(opts ...Option) Message {
msg := DEFAULT_MESSAGE
for _, o := range opts {
o(&msg)
}
return msg
}
func NewByOptionWithoutID(id int, opts ...Option) Message {
msg := DEFAULT_MESSAGE
msg.id = id
for _, o := range opts {
o(&msg)
}
return msg
}
func main() {
message1 := New(1, 123, "message1", "cache1")
message1.String()
message2 := NewByOption(
WithID(2),
WithName("message2"),
WithAddress("cache2"),
WithPhone(456),
)
message2.String()
message3 := NewByOptionWithoutID(
3,
WithAddress("cache3"),
WithPhone(789),
WithName("message3"),
)
message3.String()
main1()
}
/*
Output
ID:1
- Name:message1
- Address:cache1
- phone:123
ID:2
- Name:message2
- Address:cache2
- phone:456
ID:3
- Name:message3
- Address:cache3
- phone:789
*/
//例2
type OptionFunc func(op *OptionMenu)
type OptionMenu struct {
op1 string
op2 string
op3 int
op4 int
//......可能還會有跟多的屬性加入,這給實體化帶來了巨大的問題
}
func InitOptions(optionFuncs ...OptionFunc) OptionMenu {
option := OptionMenu{}
for _, op := range optionFuncs {
op(&option)
}
return option
}
func WithOp1(op1 string) OptionFunc {
return func(op *OptionMenu) {
op.op1 = op1
}
}
func WithOp2(op2 string) OptionFunc {
return func(op *OptionMenu) {
op.op2 = op2
}
}
func WithOp3(op3 int) OptionFunc {
return func(op *OptionMenu) {
op.op3 = op3
}
}
func WithOp4(op4 int) OptionFunc {
return func(op *OptionMenu) {
op.op4 = op4
}
}
func main1() {
op := InitOptions(
WithOp1("op1"),
WithOp2("op2"),
WithOp3(3),
WithOp4(4),
)
fmt.Printf("%#v\n", op)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/252026.html
標籤:其他
上一篇:樹莓派制作游戲機教程
