当前位置: 首页 > news >正文

网站根目录权限设置网络科技公司取名推荐

网站根目录权限设置,网络科技公司取名推荐,营销型网站建设哪家专业,html网站建设代码定义 表示一个作用于某对象结构中的各元素的操作,它使我们可以在不改变元素的类的前提下定义作用与这些元素的新操作。 模式动机 对于存储在一个集合中的对象,他们可能具有不同的类型(即使有一个公共的接口),对于该集合中的对象&#xff0…

定义

  • 表示一个作用于某对象结构中的各元素的操作,它使我们可以在不改变元素的类的前提下定义作用与这些元素的新操作。

模式动机

  • 对于存储在一个集合中的对象,他们可能具有不同的类型(即使有一个公共的接口),对于该集合中的对象,可以接受一类称为访问者的对象来访问,不同的访问者其访问方式也有所不同。

访问者模式角色

  • 抽象访问者: 一般通过visit方法声明可以访问哪写元素;
  • 具体访问者: 实现抽象访问者。访问元素的内容;
  • 抽象元素: 需要被访问的对象。一般有一个accept方法接受访问者方法;
  • 具体元素: 实现了抽象元素。在accept方法中调用访问者的visit方法;

开发中常见的场景

  • XML文挡解释器设计
  • 编译器的设计
  • 复杂集合对象的处理

总结

  • 访问者模式是GOF23种设计模式中最复杂的一种设计模式,一般在工作中不会使用访问者设计模式,也尽量不要使用访问者设计模式;如果用到了访问者设计模式,那就是必须要使用访问者设计模式啦。

代码实现

  • 抽象访问者
/*** 抽象访问者*/
public interface Visitor {void visit(ManElement manElement);void visit(WomanElement womanElement);
}
  • 具体访问者
/*** 具体访问者NameVisitor*/
public class NameVisitor implements Visitor {@Overridepublic void visit(ManElement manElement) {System.out.println("名称:"+manElement.getName());}@Overridepublic void visit(WomanElement womanElement) {System.out.println("名称:"+womanElement.getName());}
}
/*** 具体访问者GenderVisitor*/
public class GenderVisitor implements Visitor {@Overridepublic void visit(ManElement manElement) {System.out.println("性别:男");}@Overridepublic void visit(WomanElement womanElement) {System.out.println("性别:女");}
}
/*** 具体访问者AgeVisitor*/
public class AgeVisitor implements Visitor {@Overridepublic void visit(ManElement manElement) {System.out.println("年龄:"+manElement.getAge());}@Overridepublic void visit(WomanElement womanElement) {System.out.println("年龄:"+womanElement.getAge());}
}
  • 抽象元素
/*** 抽象元素*/
public interface Element {void accept(Visitor visitor);}
  • 具体元素
/*** 人*/
public interface Person {String getName();Boolean isMan();Integer getAge();
}
/*** 具体元素ManElement */
public class ManElement implements Person, Element{private String name;private Boolean isMan = true;private Integer age;public ManElement() {}public ManElement(String name, Integer age) {super();this.name = name;this.age = age;}@Overridepublic void accept(Visitor visitor) {visitor.visit(this);}@Overridepublic String getName() {return name;}@Overridepublic Boolean isMan() {return isMan;}@Overridepublic Integer getAge() {return age;}public Boolean getIsMan() {return isMan;}public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}
}
/*** 具体元素WomanElement */
public class WomanElement implements Person,Element{private String name;private Boolean isMan = false;private Integer age;@Overridepublic void accept(Visitor visitor) {visitor.visit(this);}public WomanElement() {}public WomanElement(String name, Integer age) {super();this.name = name;this.age = age;}@Overridepublic String getName() {return name;}@Overridepublic Boolean isMan() {return isMan;}@Overridepublic Integer getAge() {return age;}public Boolean getIsMan() {return isMan;}public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}
}
  • 客户端调用
public static void main(String[] args) {ManElement manElement = new ManElement("张三", 30);WomanElement womanElement = new WomanElement("翠花", 18);List<Element> elements = new ArrayList<>();elements.add(manElement);elements.add(womanElement);NameVisitor nameVisitor = new NameVisitor();GenderVisitor genderVisitor = new GenderVisitor();AgeVisitor ageVisitor = new AgeVisitor();for (Element element : elements) {element.accept(nameVisitor);element.accept(genderVisitor);element.accept(ageVisitor);}
}



更多设计模式学习:

          设计模式(1):介绍
          设计模式(2):单例模式
          设计模式(3):工厂模式
          设计模式(4):建造者模式
          设计模式(5):原型模式
          设计模式(6):桥接模式
          设计模式(7):装饰器模式
          设计模式(8):组合模式
          设计模式(9):外观模式
          设计模式(10):享元模式
          设计模式(11):适配器模式
          设计模式(12):代理模式
          设计模式(13):模板方法模式
          设计模式(14):命令模式
          设计模式(15):迭代器模式
          设计模式(16):观察者模式
          设计模式(17):中介者模式
          设计模式(18):状态模式
          设计模式(19):策略模式
          设计模式(20):责任链模式
          设计模式(21):备忘录模式
          设计模式(22):解释器模式
          设计模式(23):访问者模式

http://www.yayakq.cn/news/537237/

相关文章:

  • 米课wordpress建站中小型网站建设怎么样
  • 高端网站推广做网站三网多少钱
  • 建设个定制网站需要多少钱苏州工业园区限电
  • 深圳华丰大厦网站建设大岭山镇做网站
  • 企业网站属于广告吗wordpress 绑定手机
  • 如何招聘软件网站开发人员一个网站建设都需要什么
  • 网站建设零基础自学视频教程好看的美食怎么做视频网站
  • 怎样在网站上做有效的广告投放wordpress手机ui
  • 泸州工投建设集团有限公司网站网页源码提取工具
  • 提供郑州网站建设做电商宠物带哪个网站最好
  • 外贸网站哪家做的好整合营销推广策略
  • 做网站要会哪些知识爬取漫画数据做网站
  • 做球形全景的网站企业管理考研
  • 成都科技网站建设哪里有wordpress 酒店主题
  • 本地网站建设视频教程wordpress 群
  • 建设服装网站目的三合一网站建设平台
  • 建设飞鹰摩托车官方网站宁都网站建设
  • 优秀个人网站主页免费深圳企业名录
  • 美橙网站建设广州建设网站公司哪个济南兴田德润有活动吗
  • win2008怎么做网站福田网站建设龙岗网站建设龙岗网站建设龙岗网站建设
  • 买拆车件上什么网站域名访问网站入口
  • 做免费看电影的网站不违法吗wordpress段落
  • 什么网站比较好外贸平台网站有哪些
  • 怎么做cpa网站深圳龙华观澜网站建设公司
  • 宁波网站推广宣传公司排名男装定制品牌哪个好
  • 外贸网站建设 杭州大学院系网站建设
  • 两台电脑一台做服务器 网站深圳市建设交易中心网站首页
  • 建设银行网站下载中心竞价推广平台有哪些
  • 网站建设网站徒手整形怎么免费创建网站
  • 中山网站建设优化重庆专业网站推广报价