【设计模式】—— 访问者模式Visitor

对于某个对象或者一组对象,不同的访问者,产生的结果不同,执行操作也不同。此时,就是访问者模式的典型应用了。

  应用场景

  1 不同的子类,依赖于不同的其他对象

  2 需要对一组对象,进行许多不相关的操作,又不想在类中是现在这些方法

  3 定义的类很少改变,但是执行的操作却经常发生改变。

回到顶部

  模式结构

  Context 环境角色

复制代码
class Context{List<Node> list = new ArrayList();public void add(Node node) { list.add(node); } public void visit(Visitor visitor) { for(Node node : list){ node.accept(visitor); } } }
复制代码

  Visitor 访问者角色

复制代码
interface Visitor{public void visit(NodeA nodeA);public void visit(NodeB nodeB); } class VisitA implements Visitor{ public void visit(NodeA nodeA){ System.out.println("***visitA***"); nodeA.action(); } public void visit(NodeB nodeB){ System.out.println("***visitA***"); nodeB.action(); } } class VisitB implements Visitor{ public void visit(NodeA nodeA){ System.out.println("***visitB***"); nodeA.action(); } public void visit(NodeB nodeB){ System.out.println("***visitB***"); nodeB.action(); } }
复制代码

  Node 被访问角色

复制代码
interface Node{public void accept(Visitor visitor);
}
class NodeA implements Node{ public void accept(Visitor visitor) { visitor.visit(this); } public void action(){ System.out.println("NodeA visited"); } } class NodeB implements Node{ public void accept(Visitor visitor) { visitor.visit(this); } public void action(){ System.out.println("NodeB visited"); } }
复制代码

  全部代码

复制代码
 1 package com.xingoo.test.design.visitor;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;  5  6 interface Visitor{  7 public void visit(NodeA nodeA);  8 public void visit(NodeB nodeB);  9 } 10 class VisitA implements Visitor{ 11 public void visit(NodeA nodeA){ 12 System.out.println("***visitA***"); 13  nodeA.action(); 14  } 15 public void visit(NodeB nodeB){ 16 System.out.println("***visitA***"); 17  nodeB.action(); 18  } 19 } 20 class VisitB implements Visitor{ 21 public void visit(NodeA nodeA){ 22 System.out.println("***visitB***"); 23  nodeA.action(); 24  } 25 public void visit(NodeB nodeB){ 26 System.out.println("***visitB***"); 27  nodeB.action(); 28  } 29 } 30 interface Node{ 31 public void accept(Visitor visitor); 32 } 33 class NodeA implements Node{ 34 public void accept(Visitor visitor) { 35 visitor.visit(this); 36  } 37 public void action(){ 38 System.out.println("NodeA visited"); 39  } 40 } 41 class NodeB implements Node{ 42 public void accept(Visitor visitor) { 43 visitor.visit(this); 44  } 45 public void action(){ 46 System.out.println("NodeB visited"); 47  } 48 } 49 class Context{ 50 List<Node> list = new ArrayList(); 51 public void add(Node node) { 52  list.add(node); 53  } 54 public void visit(Visitor visitor) { 55 for(Node node : list){ 56  node.accept(visitor); 57  } 58  } 59 } 60 public class Client { 61 private static Context ctx = new Context(); 62 public static void main(String[] args) { 63 ctx.add(new NodeA()); 64 ctx.add(new NodeB()); 65 ctx.visit(new VisitA()); 66 ctx.visit(new VisitB()); 67  } 68 }
复制代码

转载于:https://www.cnblogs.com/lzcaima/p/5116966.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543749.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ruby宝石区块链最新消息_Ruby宝石| Ruby工具

ruby宝石区块链最新消息Ruby宝石 (Ruby Gems) Every language has its package manager which helps it by providing libraries and a standard format to distribute Ruby program. It is a type of tool which is developed to easily facilitate the installation of Gems.…

CMake入门(二)

CMake入门&#xff08;二&#xff09; 最后更新日期&#xff1a;2014-04-25 by kagula 阅读前提&#xff1a;《CMake入门&#xff08;一&#xff09;》、Linux的基本操作 环境: Windows 8.1 64bit英文版。Visual Studio 203 Update1英文版。CMake 2.8.12.2、 Cent OS 6.5。内容…

Scala中的评论

Scala评论 (Scala comments) Comments are things that are readable by the programmer. They are added to the code to add an explanation about the source code. Commenting on a program to make it easier to understand by the programmer. 注释是程序员可以读取的东西…

最小生成树prim (c++ 已大改)

2019独角兽企业重金招聘Python工程师标准>>> #include <iostream> #include <vector> #include <set> #include <map> #include <initializer_list> #include <memory> template<typename T> class Graph{private:std::m…

assoc_Ruby assoc()函数

assocRuby中的assoc()函数 (assoc() function in Ruby) We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays o…

前端接入HTTP协议浅析

【摘要】&#xff1a;本文整理并简要分析了HTTP协议的交互过程和内容格式&#xff0c;包括HTTP请求、HTTP应答的头域和实体内容&#xff0c;HTTP 1.0与HTTP 1.1的差异&#xff0c;并举例说明了Chunked编码的工作过程原理。1、HTTP协议简介浏览器和Web服务器之间一问一答的交互过…

互联网传真 传真指令_传真的完整形式是什么?

互联网传真 传真指令传真&#xff1a;传真 (FAX: Facsimile) FAX is an abbreviation of "Facsimile". 传真是“传真”的缩写 。 It is commonly written and spoken as FAX. It is a telephonic transmission of a scanned copy of text and images printed on a p…

C#使用七牛云存储上传下载文件、自定义回调

项目需要将音视频文件上传服务器&#xff0c;考虑并发要求高&#xff0c;通过七牛来实现。 做了一个简易的压力测试&#xff0c;同时上传多个文件&#xff0c;七牛自己应该有队列处理并发请求&#xff0c;我无论同时提交多少个文件&#xff0c;七牛是批量一个个排队处理了。 一…

netfilter que_QUE的完整形式是什么?

netfilter que问题&#xff1a;问题 (QUE: Questions) QUE is an abbreviation of "Questions". QUE是“ Questions”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email to te…

mysqld_multi stop 不能停掉mysql

使用mysqld_multi start 启动了多个mysql&#xff0c;但是mysqld_multi stop 却不能停止原因&#xff1a;因为你还没有授权/usr/local/mysql/bin/mysqld_multi stop 但是默认是停不掉的&#xff0c;需要我们做一个授权grant shutdown on *.* to usernamelocalhost identified…

热冗余冷冗余_冗余支架

热冗余冷冗余Problem statement: 问题陈述&#xff1a; Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parentheses is redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. …

对称树

Problem statement: 问题陈述&#xff1a; Given a binary Tree, check whether the tree is symmetric or not. 给定二叉树 &#xff0c; 检查树是否对称 。 Input Example: 输入示例&#xff1a; For example1/ \2 2/ \ / \3 4 4 3The above tree is symmetric1/ \2 …

unity, undo

如果在操作一个Object之前调用Undo.RecordObject(Object)&#xff0c;且操作确实造成Object某些属性的改变&#xff0c;则会产生一个undo记录。 如果我们的架构不是直接操作Object&#xff0c;而是操作一个ui&#xff0c;并在某些时机通过ui.save(Object)将数据回写到Object&am…

ajax应用_AJAX的应用

ajax应用AJAX has several benefits that can be utilized inside a web application. In this article, well explore some advantages of AJAX and see some of its applications. AJAX具有可在Web应用程序内部使用的多个优点。 在本文中&#xff0c;我们将探讨AJAX的一些优势…

Linux下DRBD配置

一、什么是DRBD1、简介 Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像&#xff1a;实时、透明、同步&#xff08;所有服务器都成功后返回&#xff09;、异步&#xff08;本地服务器成功后返回…

键盘特殊_特殊键盘

键盘特殊Problem statement: 问题陈述&#xff1a; Imagine you have a special keyboard with four types of keys: 想象一下&#xff0c;您有一个特殊的键盘&#xff0c;其中包含四种类型的键&#xff1a; Key 1: Prints I on screen 按键1&#xff1a;在屏幕上打印“ I”…

【C++入门】简单的日期类操作

//--------------------------------------------------------------------------/***名称&#xff1a;日期的简单操作******类函数&#xff1a;构造函数&#xff0c;拷贝构造函数&#xff0c;析构函数&#xff0c;操作符重载函数****日期类操作函数&#xff1a; 1&#xff1a;…

Scala山脉

Scala Range Scala山脉 A Range is a bounded series with a uniform interval with an upper and lower limit. The range literal is a numerical sequence of number ranging with a certain limit. 范围是一个有上限且下限均匀的有界序列。 范围文字是具有一定限制的范围…

黑客经验谈:跳板攻击入侵技术实例解析

网络入侵&#xff0c;安全第一,一个高明的入侵者&#xff0c;不会冒然实行动. 他们在入侵时前会做足功课&#xff0c;入侵时会通过各种技术手段保护自己&#xff0c;以防被对方发现&#xff0c;引火烧身. 其中&#xff0c;跳板技术是攻击者通常采用的技术. 下面笔者结合实例&am…

dom属性和html属性_HTML属性

dom属性和html属性Attributes are used to provide additional information of a tag such as it’s alignments, color, size of the text and other. The attributes are given with the tag that is between the angular brackets after the tag name. The attributes have …