【设计模式】第25节:行为型模式之“访问者模式”

一、简介

访问者模式允许一个或者多个操作应用到一组对象上,设计意图是解耦操作和对象本身,保持类职责单一、满足开闭原则以及应对代码的复杂性。

二、优点

  • 分离操作和数据结构
  • 增加新操作更容易
  • 集中化操作

三、适用场景

  • 数据结构稳定,操作易变
  • 一个对象的操作之间无关联

四、UML类图

请添加图片描述

五、案例

有圆形和矩形两种对象,有画图和调整大小两种数据访问者对形状进行处理。

package main

import "fmt"

type Shape interface {
	Accept(visitor Visitor)
	GetType() string
}

type Circle struct {
}

func NewCircle() *Circle {
	return &Circle{}
}

func (c *Circle) Accept(visitor Visitor) {
	visitor.Visit(c)
}

func (c *Circle) GetType() string {
	return "Circle"
}

type Rectangle struct {
}

func NewRectangle() *Rectangle {
	return &Rectangle{}
}

func (r *Rectangle) Accept(visitor Visitor) {
	visitor.Visit(r)
}

func (r *Rectangle) GetType() string {
	return "Rectangle"
}

type Visitor interface {
	Visit(shape Shape)
}

type DrawVisitor struct {
}

func NewDrawVisitor() *DrawVisitor {
	return &DrawVisitor{}
}

func (dv *DrawVisitor) Visit(shape Shape) {
	if shape.GetType() == "Circle" {
		fmt.Println("Drawing a circle")
	} else if shape.GetType() == "Rectangle" {
		fmt.Println("Drawing a rectangle")
	} else {
		fmt.Println("Unknown shape")
	}
}

type ResizeVisitor struct {
}

func NewResizeVisitor() *ResizeVisitor {
	return &ResizeVisitor{}
}

func (rv *ResizeVisitor) Visit(shape Shape) {
	if shape.GetType() == "Circle" {
		fmt.Println("Resizing a circle")
	} else if shape.GetType() == "Rectangle" {
		fmt.Println("Resizing a rectangle")
	} else {
		fmt.Println("Unknown shape")
	}
}

func main() {
	circle := NewCircle()
	rectangle := NewRectangle()
	drawVisitor := NewDrawVisitor()
	resizeVisitor := NewResizeVisitor()

	circle.Accept(drawVisitor)
	circle.Accept(resizeVisitor)
	rectangle.Accept(drawVisitor)
	rectangle.Accept(resizeVisitor)
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值