工厂模式入门

所有工厂模式都用来封装对象的创建。实际开发中,实例化一个具体对象不应该公开进行,初始化一个具体对象也经常造成“耦合”问题。适当的使用工厂模式将有助于解决复杂的依赖问题。

下面简单介绍了工厂模式的三种用法:

  • 简单工厂方法
  • 工厂方法模式
  • 抽象工厂模式

简单工厂

简单工厂其实不是一个设计模式,反而比较像是一种编程习惯。但由于经常被使用,所以有些开发人员把这个习惯误认为是“工厂模式”。

简单工厂UML类图

简单工厂代码示例

定义抽象产品接口并实现具体产品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
enum PizzaType{
case cheese
case clam
case veggie
}

protocol Pizza{
func prepare() -> Void
func bake() -> Void
func cut() -> Void
func box() -> Void
}


class CheesePizza:Pizza{
func prepare() -> Void{
print("Prepare Cheese")
}

func bake() -> Void{
print("Bake Cheese")
}

func cut() -> Void{
print("Cut Cheese Pizza")
}

func box() -> Void{
print("Box Cheese Pizza")
}
}
class ClamPizza:Pizza{
func prepare() -> Void{
print("Prepare Clam")
}

func bake() -> Void{
print("Bake Clam")
}

func cut() -> Void{
print("Cut Clam Pizza")
}

func box() -> Void{
print("Box Clam Pizza")
}
}

class VeggiePizza:Pizza{
func prepare() -> Void{
print("Prepare Veggie")
}

func bake() -> Void{
print("Bake Veggie")
}

func cut() -> Void{
print("Cut Veggie Pizza")
}

func box() -> Void{
print("Box Veggie Pizza")
}
}

实现简单工厂创建具体产品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class SimplePizzaFactory{
func createPizza(type:PizzaType) -> Pizza{
var pizza:Pizza
switch type {
case .cheese:
pizza = CheesePizza.init()
case .clam:
pizza = ClamPizza.init()
case .veggie:
pizza = VeggiePizza.init()
}
return pizza
}
}

调用简单工厂:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

class PizzaStore{
var factory:SimplePizzaFactory
init(factory:SimplePizzaFactory) {
self.factory = factory
}


func orderPizza(type:PizzaType) -> Pizza {
let pizza = factory.createPizza(type: type)
pizza.prepare()
pizza.bake()
pizza.cut()
pizza.box()
return pizza
}
}

客户调用:

1
2
3
4
5
6

let factory = SimplePizzaFactory.init()
let store = PizzaStore.init(factory: factory)
let cheese = store.orderPizza(type: .cheese)
let clam = store.orderPizza(type: .clam)
let veggie = store.orderPizza(type: .veggie)

工厂方法模式

工厂方法模式定义了一个创建对象的接口,但由子类决定要实例化的类是哪一个。工厂方法让类把实例化推迟到子类进行, 这样客户程序中关于超类的代码就和子类对象创建的代码解耦了。

工厂方法模式UMl类图

工厂方法模式代码示例

创建产品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

//产品类型
enum PizzaType{
case cheese
case clam
case veggie
}

//产品基类
class Pizza{
var name = "unknow pizza"
var dough = "unknow dough"
var sauce = "unknow sauce"

func prepare() {
print("Preparing:" + name)
print("Tossing dough:" + dough)
print("Adding Sauce:" + sauce)
}

func bake() {
print("Bake for 30 minutes")
}

func cut() {
print("Cutting the pizza")
}

func box() {
print("Place pizza in Box")
}
}

//具体产品

//京味Pizza
class BJStypeCheesePizza: Pizza {
override init() {
super.init()
name = "Beijing Cheese Pizza"
dough = "Beijing Cheese Dough"
sauce = "Beijing Cheese Sauce"
}
}

class BJStypeClamPizza: Pizza {
override init() {
super.init()
name = "Beijing Clam Pizza"
dough = "Beijing Clam Dough"
sauce = "Beijing Clam Sauce"
}
}

class BJStyleVeggiePizza: Pizza {
override init() {
super.init()
name = "Beijing Veggie Pizza"
dough = "Beijing Veggie Dough"
sauce = "Beijing Veggie Sauce"
}
}

//魔都风味Pizza
class SHStypeCheesePizza: Pizza {
override init() {
super.init()
name = "Shanghai Cheese Pizza"
dough = "Shanghai Cheese Dough"
sauce = "Shanghai Cheese Sauce"
}
}

class SHStypeClamPizza: Pizza {
override init() {
super.init()
name = "Shanghai Clam Pizza"
dough = "Shanghai Clam Dough"
sauce = "Shanghai Clam Sauce"
}
}

class SHStypeVeggiePizza: Pizza {
override init() {
super.init()
name = "Shanghai Veggie Pizza"
dough = "Shanghai Veggie Dough"
sauce = "Shanghai Veggie Sauce"
}
}

创建工厂:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//pizza工厂基类
class PizzaStore {
func orderPizza(type:PizzaType) -> Pizza? {
let pizza = self.createPizza(type: type)
pizza?.prepare()
pizza?.bake()
pizza?.cut()
pizza?.box()
return pizza
}

func createPizza(type:PizzaType) -> Pizza?{
return nil
}
}
//帝都pizza工厂
class BJPizzaStore: PizzaStore {
override func createPizza(type:PizzaType) -> Pizza? {
var pizza:Pizza?
switch type {
case .cheese:
pizza = BJStypeCheesePizza.init()
case .clam:
pizza = BJStypeClamPizza.init()
case .veggie:
pizza = BJStyleVeggiePizza.init()
}
return pizza
}
}

//魔都pizza工厂
class SHPizzaStore: PizzaStore {
override func createPizza(type:PizzaType) -> Pizza? {
var pizza:Pizza?
switch type {
case .cheese:
pizza = SHStypeCheesePizza.init()
case .clam:
pizza = SHStypeClamPizza.init()
case .veggie:
pizza = SHStypeVeggiePizza.init()
}
return pizza
}
}

订购产品:

1
2
3
4
5
6
//订购pizza
let bjPizzaStore = BJPizzaStore.init()
let bjCheesePizza = bjPizzaStore.orderPizza(type: .cheese)

let shPizzaStore = SHPizzaStore.init()
let shClamPizza = shPizzaStore.orderPizza(type: .clam)

抽象工厂模式

抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。这样依赖,客户就从具体的产品中被解耦。

抽象工厂模式UMl类图

抽象工厂模式代码示例

创建产品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Pizza类型
enum PizzaType{
case cheese
case clam
}

//抽象产品
class Pizza {
var name = "unknow"

func prepare() {
print("Preparing Pizza")
}

func bake() {
print("Bake for 30 minutes")
}

func cut() {
print("Cutting the pizza")
}

func box() {
print("Place pizza in Box")
}

}

// 具体产品
// 不同地区的原料成就了不同风味的pizza
class CheesePizza: Pizza {
var dough: Dough?
var cheese: Cheese?
var sauce: Sauce?
var ingredientFactory: PizzaIngredientFactory
init(ingredientFactory: PizzaIngredientFactory) {
self.ingredientFactory = ingredientFactory
super.init()
self.name = "Cheese Pizza"
}
override func prepare() {
dough = ingredientFactory.createDough()
sauce = ingredientFactory.createSauce()
cheese = ingredientFactory.createCheese()
print("Preparing " + name + " 配方:" + dough!.name + sauce!.name + cheese!.name)

}
}

class ClamPizza: Pizza {
var dough: Dough?
var clam: Clam?
var sauce: Sauce?
var ingredientFactory: PizzaIngredientFactory
init(ingredientFactory: PizzaIngredientFactory) {
self.ingredientFactory = ingredientFactory
super.init()
self.name = "Clam Pizza"

}
override func prepare() {
dough = ingredientFactory.createDough()
sauce = ingredientFactory.createSauce()
clam = ingredientFactory.createClam()
print("Preparing " + name + " 配方:" + dough!.name + sauce!.name + clam!.name)

}
}

创建Pizza原料:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

//抽象产品
//Pizza原料类
class Dough {
var name = "dough"

}

class Sauce {
var name = "sauce"

}

class Cheese {
var name = "cheese"
}

class Clam {
var name = "clam"
}

//具体产品
// 帝都特色原料
class BJDough: Dough {
override init() {
super.init()
name = " 来自帝都的Dough"
}
}

class BJSauce: Sauce {
override init() {
super.init()
name = " 来自帝都的Sauce"
}
}

class BJCheese: Cheese {
override init() {
super.init()
name = " 来自帝都的Cheese"
}
}

class BJClam: Clam {
override init() {
super.init()
name = " 来自帝都的Clam"
}
}


// 魔都特色原料
class SHDough: Dough {
override init() {
super.init()
name = " 来自魔都的Dough"
}
}

class SHSauce: Sauce {
override init() {
super.init()
name = " 来自魔都的Sauce"
}
}

class SHCheese: Cheese {
override init() {
super.init()
name = " 来自魔都的Cheese"
}
}

class SHClam: Clam {
override init() {
super.init()
name = " 来自魔都的Clam"
}
}

创建工厂生产原料:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// 原料工厂接口(抽象工厂模式)
protocol PizzaIngredientFactory {
func createDough() -> Dough
func createSauce() -> Sauce
func createCheese() -> Cheese
func createClam() -> Clam
}

//帝都原料工厂
class BJPizzaIngredientFactory: PizzaIngredientFactory {
func createClam() -> Clam {
return BJClam.init()
}

func createDough() -> Dough {
return BJDough.init()
}

func createSauce() -> Sauce {
return BJSauce.init()
}

func createCheese() -> Cheese {
return BJCheese.init()
}
}

//魔都原料工厂
class SHPizzaIngredientFactory: PizzaIngredientFactory {
func createClam() -> Clam {
return SHClam.init()
}

func createDough() -> Dough {
return SHDough.init()
}

func createSauce() -> Sauce {
return SHSauce.init()
}

func createCheese() -> Cheese {
return SHCheese.init()
}
}

创建工厂生产产品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//pizza工厂基类(抽象工厂的客户)
class PizzaStore {
func orderPizza(type:PizzaType) -> Pizza? {
let pizza = self.createPizza(type: type)
pizza?.prepare()
pizza?.bake()
pizza?.cut()
pizza?.box()
return pizza
}

func createPizza(type:PizzaType) -> Pizza?{
return nil
}
}

//帝都pizza工厂
class BJPizzaStore: PizzaStore {
override func createPizza(type:PizzaType) -> Pizza? {
var pizza:Pizza?
let ingredientFactory = BJPizzaIngredientFactory.init()
switch type {
case .cheese:
pizza = CheesePizza.init(ingredientFactory: ingredientFactory)
case .clam:
pizza = ClamPizza.init(ingredientFactory: ingredientFactory)
}
return pizza
}
}

//魔都pizza工厂
class SHPizzaStore: PizzaStore {
override func createPizza(type:PizzaType) -> Pizza? {
var pizza:Pizza?
let ingredientFactory = SHPizzaIngredientFactory.init()
switch type {
case .cheese:
pizza = CheesePizza.init(ingredientFactory: ingredientFactory)
case .clam:
pizza = ClamPizza.init(ingredientFactory: ingredientFactory)
}
return pizza
}
}

客户调用:

1
2
3
4
5
6
//订购pizza
let bjPizzaStore = BJPizzaStore.init()
let bjCheesePizza = bjPizzaStore.orderPizza(type: .cheese)

let shPizzaStore = SHPizzaStore.init()
let shClamPizza = shPizzaStore.orderPizza(type: .clam)