Skip to content

Commit 23cb856

Browse files
committed
update abstractfactory
1 parent a93ad36 commit 23cb856

File tree

2 files changed

+25
-32
lines changed

2 files changed

+25
-32
lines changed

05_abstract_factory/abstractfactory.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package abstractfactory
22

3+
import "fmt"
4+
35
//OrderMainDAO 为订单主记录
46
type OrderMainDAO interface {
5-
SaveOrderMain() string
7+
SaveOrderMain()
68
}
79

810
//OrderDetailDAO 为订单详情纪录
911
type OrderDetailDAO interface {
10-
SaveOrderDetail() string
12+
SaveOrderDetail()
1113
}
1214

1315
//DAOFactory DAO 抽象模式工厂接口
@@ -20,16 +22,16 @@ type DAOFactory interface {
2022
type RDBMainDAO struct{}
2123

2224
//SaveOrderMain ...
23-
func (*RDBMainDAO) SaveOrderMain() string {
24-
return "rdb main save"
25+
func (*RDBMainDAO) SaveOrderMain() {
26+
fmt.Print("rdb main save\n")
2527
}
2628

2729
//RDBDetailDAO 为关系型数据库的OrderDetailDAO实现
2830
type RDBDetailDAO struct{}
2931

3032
// SaveOrderDetail ...
31-
func (*RDBDetailDAO) SaveOrderDetail() string {
32-
return "rdb detail save"
33+
func (*RDBDetailDAO) SaveOrderDetail() {
34+
fmt.Print("rdb detail save\n")
3335
}
3436

3537
//RDBDAOFactory 是RDB 抽象工厂实现
@@ -47,16 +49,16 @@ func (*RDBDAOFactory) CreateOrderDetailDAO() OrderDetailDAO {
4749
type XMLMainDAO struct{}
4850

4951
//SaveOrderMain ...
50-
func (*XMLMainDAO) SaveOrderMain() string {
51-
return "xml main save"
52+
func (*XMLMainDAO) SaveOrderMain() {
53+
fmt.Print("xml main save\n")
5254
}
5355

5456
//XMLDetailDAO XML存储
5557
type XMLDetailDAO struct{}
5658

5759
// SaveOrderDetail ...
58-
func (*XMLDetailDAO) SaveOrderDetail() string {
59-
return "xml detail save"
60+
func (*XMLDetailDAO) SaveOrderDetail() {
61+
fmt.Print("xml detail save")
6062
}
6163

6264
//XMLDAOFactory 是RDB 抽象工厂实现
Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
package abstractfactory
22

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()
3+
func getMainAndDetail(factory DAOFactory) {
4+
factory.CreateOrderMainDAO().SaveOrderMain()
5+
factory.CreateOrderDetailDAO().SaveOrderDetail()
96
}
107

11-
func TestRdbFactory(t *testing.T) {
8+
func ExampleRdbFactory() {
129
var factory DAOFactory
1310
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-
}
11+
getMainAndDetail(factory)
12+
// Output:
13+
// rdb main save
14+
// rdb detail save
2115
}
2216

23-
func TestXmlFactory(t *testing.T) {
17+
func ExampleXmlFactory() {
2418
var factory DAOFactory
2519
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-
}
20+
getMainAndDetail(factory)
21+
// Output:
22+
// xml main save
23+
// xml detail save
3324
}

0 commit comments

Comments
 (0)