设计模式-Facade(门面模式)GO语言版本

前言

个人理解Facade模式其实日常生活中已经不知不觉就在使用了,基本核心内容就是暴露一些简单操作的接口,实现上将一些内容封装起来。

如上图,外界使用内部子系统时,只需要通过调用facade接口层面的功能,不需要了解子系统内部情况。但是实际情况上,外界也可调用一系列系统内部函数,所以子系统并不是黑盒一样被封装起来

问题

假设你必须在代码中使用某个复杂的库或框架中的众多对象。 正常情况下, 你需要负责所有对象的初始化工作、 管理其依赖关系并按正确的顺序执行方法等。

例如,有一个家庭影院系统,包含DVD、投影仪、音响等等子系统,现在想要通过小爱同学使用这个家庭影院系统,应该怎么做呢

解决方案

本质上就是使用接口进行隔离,降低耦合程度,以及使用成本。

那你总不能说小爱同学帮我打开DVD、打开投影仪、打开音响吧,每次想看个电影那也太累了。

所以聪明的已经将上面操作封装一下了,如下。

各个子系统,DVD、投影仪、音响

Go 复制代码
// Subsystem - DVDPlayer
type DVDPlayer struct{}

func (d *DVDPlayer) On() {
    fmt.Println("DVD Player is on")
}

func (d *DVDPlayer) Play(movie string) {
    fmt.Printf("DVD Player is playing %s\n", movie)
}

func (d *DVDPlayer) Off() {
    fmt.Println("DVD Player is off")
}

// Subsystem - Projector
type Projector struct{}

func (p *Projector) On() {
    fmt.Println("Projector is on")
}

func (p *Projector) SetInput(source string) {
    fmt.Printf("Projector set input to %s\n", source)
}

func (p *Projector) Off() {
    fmt.Println("Projector is off")
}

// Subsystem - SoundSystem
type SoundSystem struct{}

func (s *SoundSystem) On() {
    fmt.Println("Sound System is on")
}

func (s *SoundSystem) SetVolume(volume int) {
    fmt.Printf("Sound System volume set to %d\n", volume)
}

func (s *SoundSystem) Off() {
    fmt.Println("Sound System is off")
}

封装的facade家庭影院子系统

Go 复制代码
type HomeTheaterFacade interface {
	WatchMovie(movie string)
	EndMovie()
}

type HomeTheaterFacadeImp struct {
	dvdPlayer   *DVDPlayer
	projector   *Projector
	soundSystem *SoundSystem
}

func NewHomeTheaterFacade() HomeTheaterFacade {
	return &HomeTheaterFacadeImp{
		dvdPlayer:   &DVDPlayer{},
		projector:   &Projector{},
		soundSystem: &SoundSystem{},
	}
}

func (h *HomeTheaterFacadeImp) WatchMovie(movie string) {
	h.dvdPlayer.On()
	h.projector.On()
	h.projector.SetInput("DVD")
	h.soundSystem.On()
	h.soundSystem.SetVolume(10)
	h.dvdPlayer.Play(movie)
}

func (h *HomeTheaterFacadeImp) EndMovie() {
	h.dvdPlayer.Off()
	h.projector.Off()
	h.soundSystem.Off()
}

使用示例

Go 复制代码
type XiaoAI struct {
	homeTheater HomeTheaterFacade
}

func NewXiaoAI() *XiaoAI{
	return &XiaoAI{
        homeTheater: NewHomeTheaterFacade(),
    }
}
func (x *XiaoAI) Watch(movie string){
	x.homeTheater.WatchMovie(movie)
}

func (x *XiaoAI) End(){
	x.homeTheater.EndMovie()
}

这样你想看电影只需要对小爱同学说给我Watch一个电影,就可以了,也不用关心它是怎么放的。 所有的细节都是背后去实现的。

以上就是代码的类图之间的关系,可以看出外部只需要依赖接口而不用考虑具体实现。当然facade设计模式本身还具有很多变体,本质上是封装使用。所以如果上面的功能需要可以让小爱同学调节音量等功能,也可以在封装出调节音量的函数。

相关推荐
程序员Terry13 小时前
同事被深拷贝坑了3小时,我教他原型模式的正确打开方式
java·设计模式
刀法如飞1 天前
AI时代,程序员都应该是算法思想工程师
人工智能·设计模式·程序员
在西安放羊的牛油果2 天前
我把 2000 行下单代码,重构成了一套交易前端架构
前端·设计模式·架构
寅时码2 天前
React 正在演变为一场不可逆的赛博瘟疫:AI 投毒、编译器迷信与装死的官方
前端·react.js·设计模式
willow6 天前
Axios由浅入深
设计模式·axios
七月丶8 天前
别再手动凑 PR 了:这个 AI Skill 会按仓库习惯自动建分支、拆提交、提 PR
人工智能·设计模式·程序员
刀法如飞8 天前
从程序员到架构师:6大编程范式全解析与实践对比
设计模式·系统架构·编程范式
九狼8 天前
Flutter + Riverpod +MVI 架构下的现代状态管理
设计模式
静水流深_沧海一粟8 天前
04 | 别再写几十个参数的构造函数了——建造者模式
设计模式