抽象工厂模式用于生成产品族的工厂,所生成的对象是有关联的。
如果抽象工厂退化成生成的对象无关联则成为工厂函数模式。
比如本例子中使用RDB和XML存储订单信息,抽象工厂分别能生成相关的主订单信息和订单详情信息。 如果业务逻辑中需要替换使用的时候只需要改动工厂函数相关的类就能替换使用不同的存储方式了。
代码实现
package abstractfactoryimport "fmt"//OrderMainDAO 为订单主记录
type OrderMainDAO interface {SaveOrderMain()
}//OrderDetailDAO 为订单详情纪录
type OrderDetailDAO interface {SaveOrderDetail()
}//DAOFactory DAO 抽象模式工厂接口
type DAOFactory interface {CreateOrderMainDAO() OrderMainDAOCreateOrderDetailDAO() OrderDetailDAO
}//RDBMainDAP 为关系型数据库的OrderMainDAO实现
type RDBMainDAO struct{}//SaveOrderMain ...
func (*RDBMainDAO) SaveOrderMain() {fmt.Print("rdb main save\n")
}//RDBDetailDAO 为关系型数据库的OrderDetailDAO实现
type RDBDetailDAO struct{}// SaveOrderDetail ...
func (*RDBDetailDAO) SaveOrderDetail() {fmt.Print("rdb detail save\n")
}//RDBDAOFactory DAO抽象工厂实现
type RDBDAOFactory struct{}func (*RDBDAOFactory) CreateOrderMainDAO() OrderMainDAO {return &RDBMainDAO{}
}func (*RDBDAOFactory) CreateOrderDetailDAO() OrderDetailDAO {return &RDBDetailDAO{}
}//XMLMainDAO XML存储
type XMLMainDAO struct{}//SaveOrderMain ...
func (*XMLMainDAO) SaveOrderMain() {fmt.Print("xml main save\n")
}//XMLDetailDAO XML存储
type XMLDetailDAO struct{}// SaveOrderDetail ...
func (*XMLDetailDAO) SaveOrderDetail() {fmt.Print("xml detail save")
}//XMLDAOFactory DAO抽象工厂实现
type XMLDAOFactory struct{}func (*XMLDAOFactory) CreateOrderMainDAO() OrderMainDAO {return &XMLMainDAO{}
}func (*XMLDAOFactory) CreateOrderDetailDAO() OrderDetailDAO {return &XMLDetailDAO{}
}
单元测试
package abstractfactoryfunc getMainAndDetail(factory DAOFactory) {factory.CreateOrderMainDAO().SaveOrderMain()factory.CreateOrderDetailDAO().SaveOrderDetail()
}func ExampleRdbFactory() {var factory DAOFactoryfactory = &RDBDAOFactory{}getMainAndDetail(factory)// Output:// rdb main save// rdb detail save
}func ExampleXmlFactory() {var factory DAOFactoryfactory = &XMLDAOFactory{}getMainAndDetail(factory)// Output:// xml main save// xml detail save
}