我会尽力在这里解释我的情况。
所以,我正在为我的应用程序创建一个 DAL。它同时访问redis和mysql。
redis接口如下:
文件 NoSqlDBClient.go:
type NosqlDb interface {
HGet() string
}
type NosqlClient struct {
key string
}
func GetNosqlClient() *NosqlClient{
return &NosqlClient{}
}
func (ns *NosqlClient) HGet()string {
//actual implemenation would be different
return ns.key
}
文件 SqldbClient.go:
type SqlDB interface {
ExecQuery()
}
type SqlClient struct {
query string
}
func GetsqlClient() *SqlClient{
return &SqlClient{}
}
func (s *SqlClient) ExecQuery()string {
//actual implemenation would be different
return s.query
}
现在我需要实现一个 DBClient Factory,它维护一个 dbtype 和客户端的映射。它是这样的
文件 DBClientFactory.go
type DBClientfactory struct {
clientmap[string] //what data type to use???
}
func GetNoSqlDBClient() NosqlDb{
client:=NoSqlDBClient.GetNosqlClient()
clientmap['nosql'] = client
return client
}
func GetSqlDBClient() SqlDB{
client:=SqlDBClient.GetsqlClient()
clientmap['sql'] = client
return client
}
问题是如何在一张地图中容纳不同类型的客户?我想定义另一个接口DBFactory,其中嵌入了其他两个接口。但这显然行不通,因为所有方法都不是由单个接口实现的。
我该如何解决这个问题?
智慧大石
相关分类