Facade外观模式

Facade外观模式

封装交互,简化调用

实际场景

定义

为子系统的一组接口提供一致的界面,外观模式定义了一个高层接口,这一接口使得子系统更加容易调用

外观模式结构说明

uml

代码

package _3_facade

import "fmt"

type AModuleApi interface {
	testA()
}

type AModuleImpl struct{}

func newA() *AModuleImpl {
	return &AModuleImpl{}
}

func (AModuleImpl) testA() {
	fmt.Println("testa")
}

type BModuleApi interface {
	testB()
}

type BModuleImpl struct{}

func newB() *BModuleImpl {
	return &BModuleImpl{}
}

func (b *BModuleImpl) testB() {
	fmt.Println("testB")
}

type Facade struct {}

func newFacde() *Facade {
	return &Facade{}
}

func (f *Facade) test() {
	// 在内部实现,可以调用多个模块
	a := newA()
	a.testA()

	b := newB()
	b.testB()
}

// client = main方法
func Client() {
	facde := newFacde()
	facde.test()
}

时序图

优缺点

何时使用

相关模式

原文地址:https://www.cnblogs.com/maomaomaoge/p/14175934.html