Skip to content

Commit c97afc5

Browse files
committed
add abstract factory
1 parent 7402154 commit c97afc5

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

05_abstract_factory/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# 抽象工厂模式
2+
3+
抽象工厂模式用于生成产品族的工厂,所生成的对象是有关联的。
4+
5+
如果抽象工厂退化成生成的对象无关联则成为工厂函数模式。
6+
7+
比如本例子中使用RDB和XML存储订单信息,抽象工厂分别能生成相关的主订单信息和订单详情信息。
8+
如果业务逻辑中需要替换使用的时候只需要改动工厂函数相关的类就能替换使用不同的存储方式了。
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package abstractfactory
2+
3+
//OrderMainDAO 为订单主记录
4+
type OrderMainDAO interface {
5+
SaveOrderMain() string
6+
}
7+
8+
//OrderDetailDAO 为订单详情纪录
9+
type OrderDetailDAO interface {
10+
SaveOrderDetail() string
11+
}
12+
13+
//DAOFactory DAO 抽象模式工厂接口
14+
type DAOFactory interface {
15+
CreateOrderMainDAO() OrderMainDAO
16+
CreateOrderDetailDAO() OrderDetailDAO
17+
}
18+
19+
//RDBMainDAP 为关系型数据库的OrderMainDAO实现
20+
type RDBMainDAO struct{}
21+
22+
//SaveOrderMain ...
23+
func (*RDBMainDAO) SaveOrderMain() string {
24+
return "rdb main save"
25+
}
26+
27+
//RDBDetailDAO 为关系型数据库的OrderDetailDAO实现
28+
type RDBDetailDAO struct{}
29+
30+
// SaveOrderDetail ...
31+
func (*RDBDetailDAO) SaveOrderDetail() string {
32+
return "rdb detail save"
33+
}
34+
35+
//RDBDAOFactory 是RDB 抽象工厂实现
36+
type RDBDAOFactory struct{}
37+
38+
func (*RDBDAOFactory) CreateOrderMainDAO() OrderMainDAO {
39+
return &RDBMainDAO{}
40+
}
41+
42+
func (*RDBDAOFactory) CreateOrderDetailDAO() OrderDetailDAO {
43+
return &RDBDetailDAO{}
44+
}
45+
46+
//XMLMainDAO XML存储
47+
type XMLMainDAO struct{}
48+
49+
//SaveOrderMain ...
50+
func (*XMLMainDAO) SaveOrderMain() string {
51+
return "xml main save"
52+
}
53+
54+
//XMLDetailDAO XML存储
55+
type XMLDetailDAO struct{}
56+
57+
// SaveOrderDetail ...
58+
func (*XMLDetailDAO) SaveOrderDetail() string {
59+
return "xml detail save"
60+
}
61+
62+
//XMLDAOFactory 是RDB 抽象工厂实现
63+
type XMLDAOFactory struct{}
64+
65+
func (*XMLDAOFactory) CreateOrderMainDAO() OrderMainDAO {
66+
return &XMLMainDAO{}
67+
}
68+
69+
func (*XMLDAOFactory) CreateOrderDetailDAO() OrderDetailDAO {
70+
return &XMLDetailDAO{}
71+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package abstractfactory
2+
3+
import "testing"
4+
5+
func getMainAndDetail(factory DAOFactory) (string, string) {
6+
main := factory.CreateOrderMainDAO()
7+
detail := factory.CreateOrderDetailDAO()
8+
return main.SaveOrderMain(), detail.SaveOrderDetail()
9+
}
10+
11+
func TestRdbFactory(t *testing.T) {
12+
var factory DAOFactory
13+
factory = &RDBDAOFactory{}
14+
main, detail := getMainAndDetail(factory)
15+
if main != "rdb main save" {
16+
t.Fatal("main save fail")
17+
}
18+
if detail != "rdb detail save" {
19+
t.Fatal("detail save fail")
20+
}
21+
}
22+
23+
func TestXmlFactory(t *testing.T) {
24+
var factory DAOFactory
25+
factory = &XMLDAOFactory{}
26+
main, detail := getMainAndDetail(factory)
27+
if main != "xml main save" {
28+
t.Fatal("main save fail")
29+
}
30+
if detail != "xml detail save" {
31+
t.Fatal("detail save fail")
32+
}
33+
}

0 commit comments

Comments
 (0)