设计模式汇总(未完待续)【Go基础】

简单工厂模式

package main

import "fmt"

type Fruit interface {
	grant()
	pick()
}

type Apple struct {
	name string
}

func (a *Apple) grant() {
	fmt.Println("种植", a.name, "苹果")
}

func (a *Apple) pick() {
	fmt.Println("采摘", a.name, "苹果")
}

type Orange struct {
	name string
}

func (o *Orange) grant() {
	fmt.Println("种植", o.name, "橙子")
}

func (o *Orange) pick() {
	fmt.Println("采摘", o.name, "橙子")
}

func Newfruit(i int) Fruit {
	switch {
	case i == 1:
		return &Apple{name: "666"}
	case i == 2:
		return &Orange{name: "777"}
	}
	return nil
}

func main() {
	apple := Newfruit(1)
	apple.grant()
	apple.pick()

	orange := Newfruit(2)
	orange.grant()
	orange.pick()
}

简单工厂模式定义为:简单工厂模式又称为静态工厂方法模型,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类

工厂模式

可以看到,在上面的简单工厂模式中,虽然可以通过Newfruit函数(相当于工厂类)产生多种水果,但是如果我们的水果种类很多,则该函数中需要由大量的case语句,相当于需要修改工厂类的代码,违反了开放-封闭原则,所以在此基础上出现了工厂模式。
工厂模式定义了一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。
工厂模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的(在抽象工厂中,选择判断是通过向工厂类传递不同的参数来实现的),也就是说工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行,在我们添加产品时,原本是修改工厂类,现在是修改客户端。
示例如下:

package main

import "fmt"

type Fruit interface {
	grant()
	pick()
	sell()
}

type Factory interface {
	creatFruit() Fruit
}

type Apple struct {
	name  string
	money int
}

func (a *Apple) grant() {
	fmt.Println("种植", a.name, "苹果")
}

func (a *Apple) pick() {
	fmt.Println("采摘", a.name, "苹果")
}

func (a *Apple) sell() {
	fmt.Println("苹果的售价是", a.money)
}

type Orange struct {
	name  string
	money int
}

func (o *Orange) grant() {
	fmt.Println("种植", o.name, "橙子")
}

func (o *Orange) pick() {
	fmt.Println("采摘", o.name, "橙子")
}

func (o *Orange) sell() {
	fmt.Println("橙子的售价是", o.money)
}

type AppleFactory struct {
}

func (a *AppleFactory) creatFruit() Fruit {
	return &Apple{name: "666", money: 5}
}

type OrangeFactory struct {
}

func (a *OrangeFactory) creatFruit() Fruit {
	return &Orange{name: "777", money: 10}
}

func main() {
	var fac Factory = &AppleFactory{}
	var fru Fruit = fac.creatFruit()
	fru.grant()
	fru.pick()
	fru.sell()

	fac = &OrangeFactory{}
	fru = fac.creatFruit()
	fru.grant()
	fru.pick()
	fru.sell()
}

当需要添加新的水果时,只需要添加新的水果类及其对应的水果工厂类即可

抽象工厂模式

介绍
介绍
感觉理解抽象工厂模式的关键在于:抽象工厂模式是对工厂模式的抽象,上面两个链接个人认为有助于理解,说一下我对第二个链接中例子的理解,如果按照工厂模式去做的话,需要一个鞋子工厂和衣服工厂接口,然后鞋子工厂用阿迪和耐克去分别实现之,对于衣服工厂也同理。但是既然大家都生成衣服和鞋子,所以对其进行抽象。

单例模式

package main

import (
	"fmt"
	"sync"
)

type Singleton interface {
	dosomething()
}

type singleton struct {
}

//首字母小写 私有的 不能导出
func (s *singleton) dosomething() {
	fmt.Println("do some thing")
}

var (
	once     sync.Once
	instance *singleton
)

func GetInstance() Singleton {
	once.Do(func() {
		instance = &singleton{}
	})
	return instance
}

func main() {
	s1 := GetInstance()
	fmt.Printf("s1: %p\n", s1)
	s2 := GetInstance()
	fmt.Printf("s2: %p\n", s2)
}

关于Do方法:func (*sync.Once).Do(f func())
Do calls the function f if and only if Do is being called for the first time for this instance of Once. In other words, given

var once Once
if once.Do(f) is called multiple times, only the first call will invoke f, even if f has a different value in each invocation. A new instance of Once is required for each function to execute.

Do is intended for initialization that must be run exactly once. Since f is niladic, it may be necessary to use a function literal to capture the arguments to a function to be invoked by Do:

config.once.Do(func() { config.init(filename) })
Because no call to Do returns until the one call to f returns, if f causes Do to be called, it will deadlock.

If f panics, Do considers it to have returned; future calls of Do return without calling f.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巴塞罗那的风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值