1 Go 常用设计模式概览
2 创建型模式
2.1 单例模式
老生常谈的设计模式了,单例模式就是确保全局共享一个实例,且只需要被初始化一次的场景,而 Go 语言的 sync.Once 正好可以保证全局只被执行一次。
type Student struct { Id int64 Name string}
var ( once sync.Once defaultStudent *Student)
func NewStudent() *Student { once.Do(func() { defaultStudent = &Student{} }) return defaultStudent}
复制代码
2.2 工厂模式
2.2.1 简单工厂
顾名思义,简单工厂就是非常的简单,根据输入的参数构造出想要的实例。
type StudentConfig struct { DB string Url string Port int}
func GetStudentDao(db, url string, port int) *StudentConfig { return &StudentConfig{ Url: url, DB: db, Port: port, }}
复制代码
2.2.2 工厂方法
工厂方法模式在简单工厂的基础上增加了 if...else...或 switch 来涵盖更多的选项,核心就是将不同的操作进行封装,利用具体的产出进行操作。
type StudentDaoType int
type StudentDao interface { Create(ctx context.Context, stu Student) bool}
const ( StudentDaoA StudentDaoType = 1 StudentDaoB StudentDaoType = 2 StudentDaoC StudentDaoType = 3)
type StudentDaoAImpl struct{}
func NewStudentDaoAImpl() *StudentDaoAImpl { return &StudentDaoAImpl{}}
type StudentDaoBImpl struct{}
func NewStudentDaoBImpl() *StudentDaoBImpl { return &StudentDaoBImpl{}}
type StudentDaoCImpl struct{}
func NewStudentDaoCImpl() *StudentDaoCImpl { return &StudentDaoCImpl{}}
func (i StudentDaoAImpl) Create(ctx context.Context, stu Student) bool { fmt.Println("A") return true}
func (i StudentDaoBImpl) Create(ctx context.Context, stu Student) bool { fmt.Println("B") return true}
func (i StudentDaoCImpl) Create(ctx context.Context, stu Student) bool { fmt.Println("C") return true}
func NewStudentDao(t StudentDaoType) StudentDao { switch t { case StudentDaoA: return NewStudentDaoAImpl() case StudentDaoB: return NewStudentDaoBImpl() case StudentDaoC: return NewStudentDaoCImpl() default: return NewStudentDaoAImpl() }}
func TestFactoryMethod() { studentDao := NewStudentDao(StudentDaoC) studentDao.Create(context.TODO(), Student{})}
复制代码
2.2.3 抽象工厂
抽象工厂相比以上两种就更加的抽象,依靠高度的抽象可以创建多个具体的实例。
type Pen interface { GetPen() string}
type Book interface { GetBook() string}
type ToolFactory interface { CreatePen() Pen CreateBook() Book}
type ShopFactory struct{}
func (f *ShopFactory) CreatePen() Pen { return &BluePen{}}func (f *ShopFactory) CreateBook() Book { return &ComputerBook{}}
type BluePen struct{}type ComputerBook struct{}
func (p *BluePen) GetPen() string { return "BluePen"}func (b *ComputerBook) GetBook() string { return "ComputerBook"}
func TestAbstractFactory() { var factory ToolFactory factory = &ShopFactory{} p1 := factory.CreatePen().GetPen() p2 := factory.CreateBook().GetBook() fmt.Printf("P1:%v\n P2:%v\n", p1, p2)}
复制代码
2.2.4 三者比较
工厂方法模式只有一个抽象类,而抽象工厂模式有多个。
工厂方法模式的具体工厂类只能创建一个具体的实例,而抽象工厂模式可以创建多个。
工厂模式中的每一个形态都是针对一定问题的解决方案,工厂方法针对的是多个产品系列结构;而抽象工厂模式针对的是多个产品族结构,一个产品族内有多个产品系列。
3 小总结
因为 Go 语言并不是严格意义上的面向对象的编程语言,而且二十三种设计模式中有一部分是需要使用到面向对象封装、继承、多肽的特性的,因此 Go 语言并不是完全能够且适合二十三种设计模式,但是经常用到的设计模式也是非常重要的。
评论