mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-28 22:46:03 +03:00
finish abstractfactory for 分库分表
This commit is contained in:
parent
9f56436745
commit
9840d95a2c
@ -1,3 +1,3 @@
|
||||
# 说明
|
||||
|
||||
Option模式又称万能用于参数传递和初始化的创建模式,一般常常与New模式配合使用,同时具有Builder模式的一些特点,创建对象更灵活.
|
||||
Option模式又称傻瓜模式,常常用于参数传递,一般常常与New模式配合使用用于初始化的一个对象的外部参数.
|
@ -8,5 +8,10 @@
|
||||
|
||||
![对比图片](../../images/abstract-factorys-method.png)
|
||||
|
||||
比如本例子中使用RDB和XML存储订单信息,抽象工厂分别能生成相关的主订单信息和订单详情信息。
|
||||
如果业务逻辑中需要替换使用的时候只需要改动工厂函数相关的类就能替换使用不同的存储方式了。
|
||||
这里使用以下例子:
|
||||
|
||||
1、抽象机械制造工厂(IProduce)制造机器人和电池的例子,机器人可以干活,电池可以为机器人充电
|
||||
|
||||
2、抽象数据仓工厂(IRepository)生产订单和订单详情的例子,以分库分表的需求为基础进行演示
|
||||
|
||||
抽象工厂的好处在于可以自由替换工厂,用符合目标的需求的对象来做想要的事情
|
||||
|
@ -1,73 +0,0 @@
|
||||
package abstractfactory
|
||||
|
||||
import "fmt"
|
||||
|
||||
//OrderMainDAO 为订单主记录
|
||||
type OrderMainDAO interface {
|
||||
SaveOrderMain()
|
||||
}
|
||||
|
||||
//OrderDetailDAO 为订单详情纪录
|
||||
type OrderDetailDAO interface {
|
||||
SaveOrderDetail()
|
||||
}
|
||||
|
||||
//DAOFactory DAO 抽象模式工厂接口
|
||||
type DAOFactory interface {
|
||||
CreateOrderMainDAO() OrderMainDAO
|
||||
CreateOrderDetailDAO() 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 是RDB 抽象工厂实现
|
||||
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 是RDB 抽象工厂实现
|
||||
type XMLDAOFactory struct{}
|
||||
|
||||
func (*XMLDAOFactory) CreateOrderMainDAO() OrderMainDAO {
|
||||
return &XMLMainDAO{}
|
||||
}
|
||||
|
||||
func (*XMLDAOFactory) CreateOrderDetailDAO() OrderDetailDAO {
|
||||
return &XMLDetailDAO{}
|
||||
}
|
100
creation/05_abstract_factory/abstractfactory_db.go
Normal file
100
creation/05_abstract_factory/abstractfactory_db.go
Normal file
@ -0,0 +1,100 @@
|
||||
package abstractfactory
|
||||
|
||||
import "fmt"
|
||||
|
||||
///这里一个分库分表的场景
|
||||
///主订单,放在常规MySQL数据库
|
||||
//订单详情,单条数据量过多,所以放在NewSQL数据库
|
||||
|
||||
//Order : 订单主记录
|
||||
type Order interface {
|
||||
SaveOrder()
|
||||
//.....BALABALA..可以继续写很多接口方法
|
||||
//每个接口方法做一件事
|
||||
}
|
||||
|
||||
//OrderDetail 订单详情
|
||||
type OrderDetail interface {
|
||||
SaveOrderDetail()
|
||||
//.....BALABALA..可以继续写很多接口方法
|
||||
//每个接口方法做一件事
|
||||
}
|
||||
|
||||
//IRepository 是当抽象工厂模式例子中的关键接口
|
||||
//IRepository 返回一组数据处理对象,用于处理不同的数据类型
|
||||
//IRepository 本质是创建工作对象,但必须以接口方式返回
|
||||
type IRepository interface {
|
||||
CreateOrderWorker() Order
|
||||
CreateOrderDetailWorker() OrderDetail
|
||||
//.....BALABALA..可以继续写很多接口方法
|
||||
//每个接口方法都要返回一个接口
|
||||
}
|
||||
|
||||
////////////////////////////////
|
||||
//接口定义好了,开始进行实现和应用
|
||||
////////////////////////////////
|
||||
|
||||
//MySQLOrderWorker 处理主订单的worker
|
||||
type MySQLOrderWorker struct{}
|
||||
|
||||
//SaveOrder 实现SaveOrder接口,保存订单
|
||||
func (*MySQLOrderWorker) SaveOrder() {
|
||||
fmt.Print("MySQL save main order\n")
|
||||
}
|
||||
|
||||
//NewSQLOrderWorker 处理订单详情的worker
|
||||
type NewSQLOrderWorker struct{}
|
||||
|
||||
// SaveOrderDetail SaveOrderDetail接口,保存订单细节
|
||||
func (*NewSQLOrderWorker) SaveOrderDetail() {
|
||||
fmt.Print("NewSQL save OrderDetail\n")
|
||||
}
|
||||
|
||||
//SQLFactory 第一个工厂
|
||||
//SQLFactory SQL关系型数据库工厂实现DAOFactory接口
|
||||
type SQLFactory struct{}
|
||||
|
||||
//CreateOrderWorker 创建实现了能够保存主订单记录的工作对象
|
||||
func (*SQLFactory) CreateOrderWorker() Order {
|
||||
return &MySQLOrderWorker{}
|
||||
}
|
||||
|
||||
//CreateOrderDetailWorker 创建实现了能够保存订单详情的工作对象
|
||||
func (*SQLFactory) CreateOrderDetailWorker() OrderDetail {
|
||||
return &NewSQLOrderWorker{}
|
||||
}
|
||||
|
||||
///假设说:现在CTO说要迁移到非关系数据库,我们进行下一个实现
|
||||
///主订单,放在常规MySQL数据库
|
||||
//订单详情,单条数据量过多,所以放在NewSQL数据库
|
||||
|
||||
//MongoDBWorker 处理主订单
|
||||
//MongoDBWorker No-SQL非关系型数据库工厂实现DAOFactory接口
|
||||
type MongoDBWorker struct{}
|
||||
|
||||
//SaveOrder by MongoDBWorker ...
|
||||
func (*MongoDBWorker) SaveOrder() {
|
||||
fmt.Print("MongoDB save main order\n")
|
||||
}
|
||||
|
||||
//PouchDBWorker 处理订单细节
|
||||
type PouchDBWorker struct{}
|
||||
|
||||
// SaveOrderDetail ...
|
||||
func (*PouchDBWorker) SaveOrderDetail() {
|
||||
fmt.Print("PouchDBWorker save OrderDetail\n")
|
||||
}
|
||||
|
||||
//NoSQLFactory 第二个工厂,用于处理非关系型存储
|
||||
//NoSQLFactory No-SQL非关系型数据库工厂实现DAOFactory接口
|
||||
type NoSQLFactory struct{}
|
||||
|
||||
//CreateOrderWorker 创建主订单工作对象
|
||||
func (*NoSQLFactory) CreateOrderWorker() Order {
|
||||
return &MongoDBWorker{}
|
||||
}
|
||||
|
||||
//CreateOrderDetailWorker 创建订单详情工作对象
|
||||
func (*NoSQLFactory) CreateOrderDetailWorker() OrderDetail {
|
||||
return &PouchDBWorker{}
|
||||
}
|
@ -1,24 +1,21 @@
|
||||
package abstractfactory
|
||||
|
||||
func getMainAndDetail(factory DAOFactory) {
|
||||
factory.CreateOrderMainDAO().SaveOrderMain()
|
||||
factory.CreateOrderDetailDAO().SaveOrderDetail()
|
||||
import "testing"
|
||||
|
||||
func TestSQLFactory(t *testing.T) {
|
||||
|
||||
factory := &SQLFactory{}
|
||||
orderWorker := factory.CreateOrderWorker()
|
||||
orderWorker.SaveOrder()
|
||||
detailWorker := factory.CreateOrderDetailWorker()
|
||||
detailWorker.SaveOrderDetail()
|
||||
}
|
||||
|
||||
func ExampleRdbFactory() {
|
||||
var factory DAOFactory
|
||||
factory = &RDBDAOFactory{}
|
||||
getMainAndDetail(factory)
|
||||
// Output:
|
||||
// rdb main save
|
||||
// rdb detail save
|
||||
}
|
||||
func TestNoSqlFactory(t *testing.T) {
|
||||
|
||||
func ExampleXmlFactory() {
|
||||
var factory DAOFactory
|
||||
factory = &XMLDAOFactory{}
|
||||
getMainAndDetail(factory)
|
||||
// Output:
|
||||
// xml main save
|
||||
// xml detail save
|
||||
factory := &NoSQLFactory{}
|
||||
orderWorker := factory.CreateOrderWorker()
|
||||
orderWorker.SaveOrder()
|
||||
detailWorker := factory.CreateOrderDetailWorker()
|
||||
detailWorker.SaveOrderDetail()
|
||||
}
|
||||
|
3
creation/README.md
Normal file
3
creation/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
# creation pattern
|
||||
|
||||
创建模式,用于创建对象
|
Loading…
Reference in New Issue
Block a user