首页 > 编程语言 > 详细

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

时间:2020-03-19 12:26:58      阅读:56      评论:0      收藏:0      [点我收藏+]

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 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()

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

原文:https://www.cnblogs.com/zhaogaojian/p/12522893.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!