一、简介
门面模式,也叫外观模式,英文全称是Facade Design Pattern。门面模式为子系统提供一组统一的接口,定义一组高层接口让子系统更易用。
目的:简化复杂系统的交互方式
特点:提供一个统一的交互接口
二、UML类图
三、案例
以下案例是家庭影院,家庭影院中有各种播放设备,如DVD、音响等,整体对外提供看电影和关闭的功能。
package mainimport "fmt"type Projecter struct {
}func (Projecter) On() {fmt.Println("Turning on the projecter...")
}func (Projecter) Off() {fmt.Println("Turning off the projecter...")
}type DVDPlayer struct {
}func (DVDPlayer) Play() {fmt.Println("Playing the DVDPlayer...")
}func (DVDPlayer) Stop() {fmt.Println("Stopping the DVDPlayer...")
}type SoundSystem struct {
}func (SoundSystem) On() {fmt.Println("Turning on the sound system...")
}func (SoundSystem) Off() {fmt.Println("Turning off the sound system...")
}type HomeTheaterFacade struct {Projecter ProjecterDVDPlayer DVDPlayerSoundSystem SoundSystem
}func NewHomeTheaterFacade(projecter Projecter, dvdPlayer DVDPlayer, soundSystem SoundSystem) HomeTheaterFacade {homeTheaterFacade := HomeTheaterFacade{}homeTheaterFacade.Projecter = projecterhomeTheaterFacade.DVDPlayer = dvdPlayerhomeTheaterFacade.SoundSystem = soundSystemreturn homeTheaterFacade
}func (htf HomeTheaterFacade) WatchMovie() {htf.Projecter.On()htf.DVDPlayer.Play()htf.SoundSystem.On()
}func (htf HomeTheaterFacade) StopMovie() {htf.Projecter.Off()htf.DVDPlayer.Stop()htf.SoundSystem.Off()
}func main() {projecter := Projecter{}dvdPlayer := DVDPlayer{}soundSystem := SoundSystem{}homeTheaterFacade := NewHomeTheaterFacade(projecter, dvdPlayer, soundSystem)homeTheaterFacade.WatchMovie()homeTheaterFacade.StopMovie()
}