关于设计模式:设计模式抽象工厂模式

38次阅读

共计 1561 个字符,预计需要花费 4 分钟才能阅读完成。

工程目录

DaoFactory.go

package AbstractFactory

// 记录接口
type OrderMainDao interface {SaveOrderMain()
    //SearchOrderMain()}

// 详情接口
type OrderDetailDao interface {SaveOrderDetail()
}

// 形象工厂接口
type DaoFactory interface {CreateOrderMainDao() OrderMainDao
    CreateOrderDetailDao() OrderDetailDao}

MYSQLDetailDaoFactory.go

package AbstractFactory

import "fmt"

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{}
}

OracleFactory.go

package AbstractFactory

// 形象工厂
type OracleFactory struct {

}

func (*OracleFactory) CreateOrderMainDao() OrderMainDao {return &OracleMainDao{}
}

func (*OracleFactory) CreateOrderDetailDao() OrderDetailDao {return &OracleDetailDao{}
}

OracleDetailDaoFactory.go

package AbstractFactory

import "fmt"

type OracleMainDao struct {

}

func (*OracleMainDao) SaveOrderMain() {fmt.Println("Oracle main save")
}

type OracleDetailDao struct {

}

func (*OracleDetailDao) SaveOrderDetail() {fmt.Println("Oracle detail save")
}

abstractFactory_test.go

package AbstractFactory

import "testing"

func TestMySqlFactory_CreateOrderMainDao(t *testing.T) {
   var factory DaoFactory
   factory = &MYSQLFactory{}
   factory.CreateOrderMainDao().SaveOrderMain()
   factory.CreateOrderDetailDao().SaveOrderDetail()

   factory = &OracleFactory{}
   factory.CreateOrderDetailDao().SaveOrderDetail()
   factory.CreateOrderMainDao().SaveOrderMain()
}

正文完
 0