Visitor 访问者模式
目录
[TOC]
1. 概述
1.1. 设计模式定义
访问者模式是对象的行为模式。访问者模式的目的是封装一些施加于某种数据结构元素之上的操作。一旦这些操作需要修改的话,接受这个操作的数据结构则可以保持不变。
1.2. 自我理解
当你想要为一个对象的组合增加新的能力,且封装并不重要时,就使用访问者模式。
访问者模式适用于数据结构相对稳定的系统,它把数据结构和作用于结构上的操作之间的耦合解脱开,使得操作集合可以相对自由地演化。
1.3. 访问者模式的组成
抽象访问者(Visitor)角色:声明了一个或者多个方法操作,形成所有的具体访问者角色必须实现的接口。
具体访问者(ConcreteVisitor)角色:实现抽象访问者所声明的接口,也就是抽象访问者所声明的各个访问操作。
抽象节点(Node)角色:声明一个接受操作,接受一个访问者对象作为一个参数。
具体节点(ConcreteNode)角色:实现了抽象节点所规定的接受操作。
2. UML
3. Example
3.1. 场景
将之前的组合模式和访问者模式结合起来,部门中提供访问口,便于访问者在不更改部门,直接做你需要的操作,便于扩展。
3.2. 代码实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
/** * Created * Date: 2021/07/28 17:08 <br/> * Description: 访问者模式 * * @author jomin@loserzhao.com * @see */ public class VisitorPatternDemo { public static void main(String[] args) { Department leafDept1 = new Department("叶子部门1"); Department leafDept2 = new Department("叶子部门2"); Department leafDept3 = new Department("叶子部门3"); Department subDept1 = new Department("子部门1"); subDept1.getChildren().add(leafDept1); subDept1.getChildren().add(leafDept2); Department subDept2 = new Department("子部门2"); subDept2.getChildren().add(leafDept3); Department parentDept = new Department("父部门"); parentDept.getChildren().add(subDept1); parentDept.getChildren().add(subDept2); Visitor removeVisitor = new RemoveVisitor(); parentDept.accept(removeVisitor); Visitor updateStatusVisitor = new UpdateStatusVisitor("禁用"); parentDept.accept(updateStatusVisitor); } public static class Department { private String name; private List<Department> children = new ArrayList<Department>(); public Department(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Department> getChildren() { return children; } public void setChildren(List<Department> children) { this.children = children; } public void accept(Visitor visitor) { visitor.visit(this); } } public interface Visitor { void visit(Department dept); } public static class RemoveVisitor implements Visitor { public void visit(Department dept) { if(dept.getChildren().size() > 0) { for(Department child : dept.getChildren()) { child.accept(this); } } System.out.println("删除部门【" + dept.getName() + "】"); } } public static class UpdateStatusVisitor implements Visitor { private String status; public void visit(Department dept) { if(dept.getChildren().size() > 0) { for(Department child : dept.getChildren()) { child.accept(this); } } System.out.println("将部门【" + dept.getName() + "】的状态修改为:" + status); } public UpdateStatusVisitor(String status) { this.status = status; } } } |
原创文章,转载请注明: 转载自LoserZhao – 诗和远方[ http://www.loserzhao.com/ ]
本文链接地址: http://www.loserzhao.com/java/designpattern/visitor-disignpattern.html
文章的脚注信息由WordPress的wp-posturl插件自动生成
0 条评论。