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

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

// 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 (r *ShapeMaker) drawCircle() {
	r.circle.draw()
}
func (r *ShapeMaker) drawRectangle() {
	r.rectangle.draw()
}
func main() {
	var shapeMaker ShapeMaker
	shapeMaker.drawCircle()
	shapeMaker.drawRectangle()

}

  

猜你喜欢

转载自www.cnblogs.com/zhaogaojian/p/12522893.html