当系统存在一个复杂的子系统,这个子系统有很多不同的部分,如果我们在外部想要使用这个子系统,外部系统需要分别和子系统的中的多个部分进行交互,这会造成很多麻烦。
如果我们把复杂子系统的多个部分抽象成一个统一的接口,不管外部是什么,只要统一调用这个接口就能和子系统交互,这样就能大大降低系统的耦合。
首先我们来模拟一个子系统,最简单的用mvc作为一个子系统,即子系统中包含模型层,视图层,控制层。
type Module struct {}
func getModeule() *Module {
return &Module{}
}
type View struct {}
func getView() *View {
return &View{}
}
type Controller struct {}
func getController() *Controller {
return &Controller{}
}使用一个接口统一使用这个MVC子系统:
type MVC struct {
module *Module
view *View
controller *Controller
}
func getMVC() *MVC{
return &MVC{getModeule(), getView(), getController()}
}