go语言挺简洁的,学习设计模式够用了,外观模式(Facade Pattern)隐藏系统的复杂性,并向客户端提供了一个客户端可以访问系统的接口。这种类型的设计模式属于结构型模式,它向现有的系统添加一个接口,来隐藏系统的复杂性。 这种模式涉及到一个单一的类,该类提供了客户端请求的简化方法和对现有系统类方法的委托调用。感觉和工厂模式有一定相似之处,但这个主要是为了隐藏系统复杂性。

工具:

设计模式 go语言实践-5 外观模式

// test project main.go
package main

import (
	"fmt"
)

type Shape interface {
	draw()
}
type Rectangle struct {
}

type Circle struct {
}

func (r *Rectangle) draw() {
	fmt.Println("Rectangle::draw()")
}

func (r *Circle) draw() {
	fmt.Println("Circle::draw()")
}

type ShapeMaker struct {
	circle    Circle
	rectangle Rectangle
}

func Draw(shape Shape) {
	shape.draw()
}
func (shapeMaker *ShapeMaker) drawCircle() {
	Draw(&shapeMaker.circle)
}
func (shapeMaker *ShapeMaker) drawRectangle() {
	Draw(&shapeMaker.rectangle)
}
func main() {
	var s ShapeMaker
	s.drawCircle()
	s.drawRectangle()

}

  运行结果:

Circle::draw()

Rectangle::draw()

相关文章:

  • 2021-05-05
  • 2021-08-20
  • 2021-07-28
猜你喜欢
  • 2021-05-20
  • 2022-01-01
  • 2022-12-23
  • 2021-04-26
  • 2021-09-23
  • 2022-12-23
  • 2021-12-30
相关资源
相似解决方案