创建DAOFactory.go
package AbstractFactory // mysql // sqlserver // oracle // 订单 // 订单报表 type OrderMainDAO interface { // 订单记录 SaveOrderMain() // 保存 //DeleteOrderMain() //SearchOrderMain() } type OrderDetailDAO interface { // 订单详情 SaveOrderDetail() // 保存 } type DAOFactory interface { // 抽象工厂接口 CreateOrderMainDAO() OrderMainDAO CreateOrderDetailDAO() OrderDetailDAO }
创建MySQLDAOFactory.go
package AbstractFactory import "fmt" // mysql针对两个接口的实现 type MySQLMainDAO struct { } func (*MySQLMainDAO) SaveOrderMain() { fmt.Println("Mysql main save") } type MySQLDetailDAO struct { } func (*MySQLDetailDAO) SaveOrderDetail() { fmt.Println("mysql detail save") }
创建MySQLFactory.go
package AbstractFactory // 抽象工厂 type MySQLFactory struct { } func (*MySQLFactory) CreateOrderMainDAO() OrderMainDAO { return &MySQLMainDAO{} } func (*MySQLFactory) CreateOrderDetailDAO() OrderDetailDAO { return &MySQLDetailDAO{} }
创建OracleDAOFactory.go
package AbstractFactory import "fmt" // mysql针对两个接口的实现 type OracleMainDAO struct { } func (*OracleMainDAO) SaveOrderMain() { fmt.Println("oracle main save") } type OracleDetailDAO struct { } func (*OracleDetailDAO) SaveOrderDetail() { fmt.Println("oracle detail save") }
创建OracleFactory.go
package AbstractFactory // 抽象工厂 type OracleFactory struct { } func (*OracleFactory) CreateOrderMainDAO() OrderMainDAO { return &OracleMainDAO{} } func (*OracleFactory) CreateOrderDetailDAO() OrderDetailDAO { return &OracleDetailDAO{} }
main.go
package main import "test/design/AbstractFactory" func main() { var factory AbstractFactory.DAOFactory //factory = &AbstractFactory.MySQLFactory{} factory = &AbstractFactory.OracleFactory{} factory.CreateOrderMainDAO().SaveOrderMain() factory.CreateOrderDetailDAO().SaveOrderDetail() }