go: Functional Options Pattern

项目结爸:

image

 

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:50
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : biz_config.go
*/
package config

// JewelryBusinessConfig 珠宝全业务配置(行业默认值)
type JewelryBusinessConfig struct {
	// 原料采购核验
	MaterialCheck    bool
	MaterialStandard string
	SupplierAudit    bool

	// 设计制图
	DesignSoftware string
	DesignReview   bool
	CustomDesign   bool

	// 加工生产
	ProductionLine     string
	CraftLevel         string
	QualityControlNode int

	// 质检
	FinalInspection    bool
	InspectionStandard string
	ReworkAllowed      bool

	// 包装
	LuxuryPackaging bool
	GiftBoxType     string
	AntiCounterfeit bool

	// 物流
	ExpressCompany        string
	InsuredValue          bool
	InternationalShipping bool

	// 财务
	AutoInvoice  bool
	TaxRate      float64
	PaymentTerms string

	// 营销
	OnlineMarketing   bool
	OfflineStore      bool
	AdvertisingBudget int

	// 业务
	OrderManagement  bool
	AfterSaleService bool
	VipService       bool

	// 人事
	StaffTraining    bool
	AttendanceSystem bool
	OfficeSupplies   bool

	// IT
	CloudServer    bool
	DataBackup     bool
	SystemSecurity bool
}

// NewDefaultConfig 返回默认企业标准配置
func NewDefaultConfig() *JewelryBusinessConfig {
	return &JewelryBusinessConfig{
		// 原料
		MaterialCheck:    true,
		MaterialStandard: "GIA",
		SupplierAudit:    true,

		// 设计
		DesignSoftware: "JewelCAD",
		DesignReview:   true,
		CustomDesign:   false,

		// 生产
		ProductionLine:     "标准生产线",
		CraftLevel:         "普通工艺",
		QualityControlNode: 3,

		// 质检
		FinalInspection:    true,
		InspectionStandard: "国标",
		ReworkAllowed:      true,

		// 包装
		LuxuryPackaging: false,
		GiftBoxType:     "基础礼盒",
		AntiCounterfeit: true,

		// 物流
		ExpressCompany:        "顺丰",
		InsuredValue:          true,
		InternationalShipping: false,

		// 财务
		AutoInvoice:  true,
		TaxRate:      0.13,
		PaymentTerms: "款到发货",

		// 营销
		OnlineMarketing: true,
		OfflineStore:    true,

		// 业务
		OrderManagement:  true,
		AfterSaleService: true,

		// 人事
		StaffTraining:    true,
		AttendanceSystem: true,
		OfficeSupplies:   true,

		// IT
		CloudServer:    true,
		DataBackup:     true,
		SystemSecurity: true,
	}
}




/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:51
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : material.go
*/
package options

import "godesginpattern/functionaloptions/config"

type Option func(*config.JewelryBusinessConfig)

func DisableMaterialCheck() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.MaterialCheck = false
	}
}

func SetMaterialStandard(standard string) Option {
	return func(c *config.JewelryBusinessConfig) {
		c.MaterialStandard = standard
	}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:52
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : design.go
*/
package options

import "godesginpattern/functionaloptions/config"

func UseCustomDesign() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.CustomDesign = true
	}
}

func SetDesignSoftware(soft string) Option {
	return func(c *config.JewelryBusinessConfig) {
		c.DesignSoftware = soft
	}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:52
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : produce.go
*/
package options

import "godesginpattern/functionaloptions/config"

func SetProductionLine(line string) Option {
	return func(c *config.JewelryBusinessConfig) {
		c.ProductionLine = line
	}
}

func UpgradeCraft() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.CraftLevel = "高级工艺"
		c.QualityControlNode = 5
	}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:53
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : inspect.go
*/
package options

import "godesginpattern/functionaloptions/config"

func SetInspectionStandard(standard string) Option {
	return func(c *config.JewelryBusinessConfig) {
		c.InspectionStandard = standard
	}
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:54
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : package.go
*/
package options

import "godesginpattern/functionaloptions/config"

func EnableLuxuryPackaging() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.LuxuryPackaging = true
		c.GiftBoxType = "奢华礼盒"
	}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:54
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : logistics.go
*/
package options

import "godesginpattern/functionaloptions/config"

func EnableInternationalShipping() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.InternationalShipping = true
	}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:55
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : finance.go
*/
package options

import "godesginpattern/functionaloptions/config"

func SetTaxRate(rate float64) Option {
	return func(c *config.JewelryBusinessConfig) {
		c.TaxRate = rate
	}
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:56
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : marketing.go
*/
package options

import "godesginpattern/functionaloptions/config"

func SetAdvertisingBudget(budget int) Option {
	return func(c *config.JewelryBusinessConfig) {
		c.AdvertisingBudget = budget
	}
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:56
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : business.go
*/
package options

import "godesginpattern/functionaloptions/config"

func EnableVipService() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.VipService = true
	}
}



/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:57
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : hr.go
*/
package options

import "godesginpattern/functionaloptions/config"

func DisableStaffTraining() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.StaffTraining = false
	}
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:57
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : it.go
*/
package options

import "godesginpattern/functionaloptions/config"

func DisableCloudServer() Option {
	return func(c *config.JewelryBusinessConfig) {
		c.CloudServer = false
	}
}

  

 

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:58
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : biz_service.go
*/
package service

import (
    "godesginpattern/functionaloptions/config"
    "godesginpattern/functionaloptions/factory"
    "godesginpattern/functionaloptions/options"
)

// GetStandardStoreConfig 标准门店
func GetStandardStoreConfig() *config.JewelryBusinessConfig {
    return factory.CreateConfig()
}

// GetLuxuryBrandConfig 高端定制品牌
func GetLuxuryBrandConfig() *config.JewelryBusinessConfig {
    return factory.CreateConfig(
       options.SetMaterialStandard("GIA+国检双标准"),
       options.UseCustomDesign(),
       options.SetDesignSoftware("Rhino+Matrix"),
       options.SetProductionLine("高端定制生产线"),
       options.UpgradeCraft(),
       options.SetInspectionStandard("奢侈品级"),
       options.EnableLuxuryPackaging(),
       options.EnableInternationalShipping(),
       options.EnableVipService(),
       options.SetAdvertisingBudget(1000000),
    )
}

// GetWholesaleFactoryConfig 批发工厂
func GetWholesaleFactoryConfig() *config.JewelryBusinessConfig {
    return factory.CreateConfig(
       options.DisableMaterialCheck(),
       options.DisableStaffTraining(),
       options.DisableCloudServer(),
       options.SetTaxRate(0.09),
    )
}


/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:58
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : config_factory.go
*/
package factory

import (
	"godesginpattern/functionaloptions/config"
	"godesginpattern/functionaloptions/options"
)

// CreateConfig 函数式选项核心工厂
func CreateConfig(opts ...options.Option) *config.JewelryBusinessConfig {
	cfg := config.NewDefaultConfig()
	for _, opt := range opts {
		opt(cfg)
	}
	return cfg
}

  

调用:

/*
# 版权所有  2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Functional Options Pattern 函数式选项模式
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : goLang 2024.3.6 go 26.2
# os        : windows 10
# database  : mysql 9.0 sql server 2019, postgreSQL 17.0  Oracle 21c Neo4j
# Datetime  : 2026/7/4 21:59
# User      :  geovindu
# Product   : GoLand
# Project   : godesginpattern
# File      : functionaloptionsbll.go
*/
package bll

import (
	"fmt"
	"godesginpattern/functionaloptions/service"
)

func FunctionaloptionsMain() {
	fmt.Println("=============================================")
	fmt.Println("【场景1:标准珠宝门店 - 默认配置】")
	standard := service.GetStandardStoreConfig()
	fmt.Printf("设计软件:%s\n", standard.DesignSoftware)
	fmt.Printf("质检标准:%s\n", standard.InspectionStandard)
	fmt.Printf("基础税率:%.2f\n", standard.TaxRate)

	fmt.Println("\n=============================================")
	fmt.Println("【场景2:高端定制珠宝品牌】")
	luxury := service.GetLuxuryBrandConfig()
	fmt.Printf("定制设计开启:%t\n", luxury.CustomDesign)
	fmt.Printf("工艺等级:%s\n", luxury.CraftLevel)
	fmt.Printf("礼盒类型:%s\n", luxury.GiftBoxType)
	fmt.Printf("VIP服务:%t\n", luxury.VipService)
	fmt.Printf("营销预算:%d\n", luxury.AdvertisingBudget)

	fmt.Println("\n=============================================")
	fmt.Println("【场景3:批发工厂精简配置】")
	factory := service.GetWholesaleFactoryConfig()
	fmt.Printf("原料核验关闭:%t\n", factory.MaterialCheck)
	fmt.Printf("云服务器:%t\n", factory.CloudServer)
	fmt.Printf("工厂税率:%.2f\n", factory.TaxRate)
}

  

输出:

 

c7e26f3a301c2f8f0b9d44f731851f44

 

posted @ 2026-07-04 22:08  ®Geovin Du Dream Park™  阅读(4)  评论(0)    收藏  举报