Skip to content

Instantly share code, notes, and snippets.

@jfeng45
Last active July 25, 2019 12:05
Show Gist options
  • Save jfeng45/5b141ec3b18118f526b88e5b23914c7c to your computer and use it in GitHub Desktop.
Save jfeng45/5b141ec3b18118f526b88e5b23914c7c to your computer and use it in GitHub Desktop.
database factory
// To map "database code" to "database interface builder"
// Concreate builder is in corresponding factory file. For example, "sqlFactory" is in "sqlFactory".go
var dsFbMap = map[string]dsFbInterface{
config.SQLDB: &sqlFactory{},
config.COUCHDB: &couchdbFactory{},
config.CACHE_GRPC: &cacheGrpcFactory{},
}
// DataStoreInterface serve as a marker to indicate the return type for Build method
type DataStoreInterface interface{}
// The builder interface for factory method pattern
// Every factory needs to implement Build method
type dsFbInterface interface {
Build(container.Container, *config.DataStoreConfig) (DataStoreInterface, error)
}
//GetDataStoreFb is accessors for factoryBuilderMap
func GetDataStoreFb(key string) dsFbInterface {
return dsFbMap[key]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment