go-pattern-examples/structure/01_facade/facade.go

60 lines
905 B
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package facade
import "fmt"
func NewAPI() API {
return &apiImpl{
a: NewAModuleAPI(),
b: NewBModuleAPI(),
}
}
//API is facade interface of facade package
type API interface {
Test() string
}
//facade implement
type apiImpl struct {
a AModuleAPI
b BModuleAPI
}
func (a *apiImpl) Test() string {
aRet := a.a.TestA()
bRet := a.b.TestB()
return fmt.Sprintf("%s\n%s", aRet, bRet)
}
//NewAModuleAPI return new AModuleAPI
func NewAModuleAPI() AModuleAPI {
return &aModuleImpl{}
}
//AModuleAPI ...
type AModuleAPI interface {
TestA() string
}
type aModuleImpl struct{}
func (*aModuleImpl) TestA() string {
return "A module running"
}
//NewBModuleAPI return new BModuleAPI
func NewBModuleAPI() BModuleAPI {
return &bModuleImpl{}
}
//BModuleAPI ...
type BModuleAPI interface {
TestB() string
}
type bModuleImpl struct{}
func (*bModuleImpl) TestB() string {
return "B module running"
}