Skip to content

Commit

Permalink
update abstractfactory
Browse files Browse the repository at this point in the history
  • Loading branch information
senghoo committed Mar 31, 2016
1 parent a93ad36 commit 23cb856
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
22 changes: 12 additions & 10 deletions 05_abstract_factory/abstractfactory.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package abstractfactory

import "fmt"

//OrderMainDAO 为订单主记录
type OrderMainDAO interface {
SaveOrderMain() string
SaveOrderMain()
}

//OrderDetailDAO 为订单详情纪录
type OrderDetailDAO interface {
SaveOrderDetail() string
SaveOrderDetail()
}

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

//SaveOrderMain ...
func (*RDBMainDAO) SaveOrderMain() string {
return "rdb main save"
func (*RDBMainDAO) SaveOrderMain() {
fmt.Print("rdb main save\n")
}

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

// SaveOrderDetail ...
func (*RDBDetailDAO) SaveOrderDetail() string {
return "rdb detail save"
func (*RDBDetailDAO) SaveOrderDetail() {
fmt.Print("rdb detail save\n")
}

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

//SaveOrderMain ...
func (*XMLMainDAO) SaveOrderMain() string {
return "xml main save"
func (*XMLMainDAO) SaveOrderMain() {
fmt.Print("xml main save\n")
}

//XMLDetailDAO XML存储
type XMLDetailDAO struct{}

// SaveOrderDetail ...
func (*XMLDetailDAO) SaveOrderDetail() string {
return "xml detail save"
func (*XMLDetailDAO) SaveOrderDetail() {
fmt.Print("xml detail save")
}

//XMLDAOFactory 是RDB 抽象工厂实现
Expand Down
35 changes: 13 additions & 22 deletions 05_abstract_factory/abstractfactory_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
package abstractfactory

import "testing"

func getMainAndDetail(factory DAOFactory) (string, string) {
main := factory.CreateOrderMainDAO()
detail := factory.CreateOrderDetailDAO()
return main.SaveOrderMain(), detail.SaveOrderDetail()
func getMainAndDetail(factory DAOFactory) {
factory.CreateOrderMainDAO().SaveOrderMain()
factory.CreateOrderDetailDAO().SaveOrderDetail()
}

func TestRdbFactory(t *testing.T) {
func ExampleRdbFactory() {
var factory DAOFactory
factory = &RDBDAOFactory{}
main, detail := getMainAndDetail(factory)
if main != "rdb main save" {
t.Fatal("main save fail")
}
if detail != "rdb detail save" {
t.Fatal("detail save fail")
}
getMainAndDetail(factory)
// Output:
// rdb main save
// rdb detail save
}

func TestXmlFactory(t *testing.T) {
func ExampleXmlFactory() {
var factory DAOFactory
factory = &XMLDAOFactory{}
main, detail := getMainAndDetail(factory)
if main != "xml main save" {
t.Fatal("main save fail")
}
if detail != "xml detail save" {
t.Fatal("detail save fail")
}
getMainAndDetail(factory)
// Output:
// xml main save
// xml detail save
}

0 comments on commit 23cb856

Please sign in to comment.