Abstract Factory Pattern

本文讨论了 Abstract Factory Pattern 抽象工厂模式的 Go 实现

Abstract Factory Pattern 抽象工厂

  • 有利于产品族的扩展,不利于当个产品族内的产品扩展

接口实现

  • 抽象工厂接口
1
2
3
4
5
6
package afbase

type AbstractFactory interface {
CreateColor() Color
CreateShape() Shape
}
  • color 接口
1
2
3
4
5
package afbase

type Color interface {
Fill()
}
  • shape 接口
1
2
3
4
5
package afbase

type Shape interface {
Draw()
}

产品族的实现

产品族01

  • 产品族01的具体工厂实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package product01

type product01Factory struct {
afbase.AbstractFactory
}

var once sync.Once
var instance *product01Factory = nil

func CreateProduct01Factory() afbase.AbstractFactory {
once.Do(func() {
instance = new(product01Factory)
})
return instance
}

func (p *product01Factory) CreateColor() afbase.Color {
return new(blue)
}

func (p *product01Factory) CreateShape() afbase.Shape {
return new(rectangle)
}
  • 产品族01的产品:rectangle 和 blue
1
2
3
4
5
6
7
type rectangle struct {
afbase.Shape
}

func (p *rectangle) Draw() {
fmt.Println("Inside Rectangle::draw() method.")
}
1
2
3
4
5
6
7
type blue struct {
afbase.Color
}

func (p *blue) Fill() {
fmt.Println("Inside Blue::fill() method.")
}

产品族02

  • 产品族02的具体工厂实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package product02

type product02Factory struct {
afbase.AbstractFactory
}

var once sync.Once
var instance *product02Factory = nil

func CreateProduct02Factory() afbase.AbstractFactory {
once.Do(func() {
instance = new(product02Factory)
})
return instance
}

func (p *product02Factory) CreateColor() afbase.Color {
return new(green)
}

func (p *product02Factory) CreateShape() afbase.Shape {
return new(circle)
}
  • 产品族02的产品:circle 和 green
1
2
3
4
5
6
7
type circle struct {
afbase.Shape
}

func (p *circle) Draw() {
fmt.Println("Inside Circle::draw() method.")
}
1
2
3
4
5
6
7
type green struct {
afbase.Color
}

func (p *green) Fill() {
fmt.Println("Inside Green::fill() method.")
}

产品族03

  • 产品族03的具体工厂实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package product03

type product03Factory struct {
afbase.AbstractFactory
}

var once sync.Once
var instance *product03Factory = nil

func CreateProduct03Factory() afbase.AbstractFactory {
once.Do(func() {
instance = new(product03Factory)
})
return instance
}

func (p *product03Factory) CreateColor() afbase.Color {
return new(red)
}

func (p *product03Factory) CreateShape() afbase.Shape {
return new(square)
}
  • 产品族03的产品:square 和 color
1
2
3
4
5
6
7
type square struct {
afbase.Shape
}

func (p *square) Draw() {
fmt.Println("Inside Square::draw() method.")
}
1
2
3
4
5
6
7
type red struct {
afbase.Color
}

func (p *red) Fill() {
fmt.Println("Inside Red::fill() method.")
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
func main() {
fmt.Println("product01: ")
prd01 := product01.CreateProduct01Factory()
c01 := prd01.CreateColor()
c01.Fill()
s01 := prd01.CreateShape()
s01.Draw()

fmt.Println("\nproduct02: ")
prd02 := product02.CreateProduct02Factory()
c02 := prd02.CreateColor()
c02.Fill()
s02 := prd02.CreateShape()
s02.Draw()

fmt.Println("\nproduct03: ")
prd03 := product03.CreateProduct03Factory()
c03 := prd03.CreateColor()
c03.Fill()
s03 := prd03.CreateShape()
s03.Draw()
}