单例模式
type singleton03 struct {
}
var ins03 *singleton03
var once sync.Once
func GetInsOr03() *singleton03 {
once.Do(func() {
ins03 = &singleton03{}
})
return ins03
}
工厂模式
type Person03 struct {
name string
age int
}
func NewPersonFactory(age int) func(name string) Person03 {
return func(name string) Person03 {
return Person03{
name: name,
age: age,
}
}
}
func use() {
newBaby := NewPersonFactory(1)
baby := newBaby("john")
fmt.Print(baby)
newTeenager := NewPersonFactory(16)
teen := newTeenager("jill")
fmt.Print(teen)
}
策略模式
type IStrategy interface {
do(int, int) int
}
type add struct{}
func (*add) do(a, b int) int {
return a + b
}
type reduce struct{}
func (*reduce) do(a, b int) int {
return a - b
}
type Operator struct {
strategy IStrategy
}
func (operator *Operator) setStrategy(strategy IStrategy) {
operator.strategy = strategy
}
func (operator *Operator) calculate(a, b int) int {
return operator.strategy.do(a, b)
}
模版模式
type Cooker interface {
fire()
cooke()
outfire()
}
type CookMenu struct {
}
func (CookMenu) fire() {
fmt.Println("开火")
}
func (CookMenu) cooke() {
}
func (CookMenu) outfire() {
fmt.Println("关火")
}
func doCook(cook Cooker) {
cook.fire()
cook.cooke()
cook.outfire()
}
type XiHongShi struct {
CookMenu
}
func (*XiHongShi) cooke() {
fmt.Println("做西红柿")
}
type ChaoJiDan struct {
CookMenu
}
func (ChaoJiDan) cooke() {
fmt.Println("做炒鸡蛋")
}
代理模式
type Seller interface {
sell(name string)
}
type Station struct {
stock int
}
func (station *Station) sell(name string) {
if station.stock > 0 {
station.stock--
fmt.Printf("代理点中:%s买了一张票,剩余:%d \n", name, station.stock)
} else {
fmt.Println("票已售空")
}
}
type StationProxy struct {
station *Station
}
func (proxy *StationProxy) sell(name string) {
fmt.Print("代理模式代理购票开始")
proxy.station.sell(name)
fmt.Print("代理模式代理购票结束")
}
选项模式
type Connection struct {
addr string
cache bool
timeout time.Duration
}
const (
defaultTimeout = 10
defaultCaching = false
)
type options struct {
timeout time.Duration
caching bool
}
type Option interface {
apply(*options)
}
type optionFunc func(*options)
func (f optionFunc) apply(o *options) {
f(o)
}
func WithTimeout(t time.Duration) Option {
return optionFunc(func(o *options) {
o.timeout = t
})
}
func WithCaching(cache bool) Option {
return optionFunc(func(o *options) {
o.caching = cache
})
}
func Connect(addr string, opts ...Option) (*Connection, error) {
options := options{
timeout: defaultTimeout,
caching: defaultCaching,
}
for _, o := range opts {
o.apply(&options)
}
return &Connection{
addr: addr,
cache: options.caching,
timeout: options.timeout,
}, nil
}