zoukankan      html  css  js  c++  java
  • 设计模式(二十)——访问者模式

    1.描述

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

    2.模式的使用

    ·抽象元素(Element):一个抽象类,该类定义了接受访问者的accept()操作。

    ·具体元素(Concrete Element)Element的子类。

    ·对象结构(Object Structure):一个集合,用于存放Element对象,提供遍历他自己的方法。

    ·抽象访问者(Visitor):一个接口,该接口定义操作对象(具体元素的实例)的方法

    ·具体访问者(Concrete Visitor):实现Visitor接口的类。

    3.使用情景

    ·一个对象结构中包含很多对象,想对集合中的对象增加一些新的操作。

    ·需要对集合中的对象进行很多不同且不相关的操作,而又不想修改对象的类。

    4.优点

    ·可以在不改变一个集合种元素的类的情况下,增加新的作用于该元素上的新操作。

    ·可以将集合中各个元素的某些操作集中到访问者中,便于集合的维护和元素的复用。

    5.UML

    6案例

    对本科生有数学,英语成绩;研究生有英语、数学、物理成绩;公司(Visitor)通过学会成绩判断是否录取。

      1 package 访问者模式;
      2 
      3 import java.util.ArrayList;
      4 
      5 public class test1 {
      6 
      7     public static void main(String[] args) {
      8         ArrayList<Student> list = new ArrayList<Student>();
      9         list.add(new Undergraduate("张三", 82, 92));
     10         list.add(new Undergraduate("李四", 79, 92));
     11         list.add(new GraduateStudent(73, 82, 92, "王五"));
     12         list.add(new GraduateStudent(93, 82, 92, "赵六"));
     13         Company v = new Company(); 
     14         for(Student s : list)
     15             s.accept(v);
     16     }
     17 
     18 }
     19 
     20 /*
     21  * 抽象元素
     22  */
     23 abstract class Student{
     24     public abstract void accept(Visitor v);
     25 }
     26 
     27 /*
     28  * 具体元素
     29  */
     30 class Undergraduate extends Student{
     31     double math, english;//成绩
     32     String name;
     33     
     34     Undergraduate(String name, double math, double english){
     35         this.math = math;
     36         this.english = english;
     37         this.name = name;
     38     }
     39     
     40     @Override
     41     public void accept(Visitor v) {
     42         v.visit(this);
     43     }
     44 
     45     public double getMath() {
     46         return math;
     47     }
     48 
     49     public double getEnglish() {
     50         return english;
     51     }
     52 
     53     public String getName() {
     54         return name;
     55     }
     56     
     57 }
     58 
     59 class GraduateStudent extends Student{
     60     double math, english, physics;//成绩
     61     String name;
     62     GraduateStudent(double math, double english, double physics, String name){
     63         this.math = math;
     64         this.english = english;
     65         this.physics = physics;
     66         this.name = name;
     67     }
     68     
     69     
     70     public double getMath() {
     71         return math;
     72     }
     73 
     74 
     75     public double getEnglish() {
     76         return english;
     77     }
     78 
     79 
     80     public double getPhysics() {
     81         return physics;
     82     }
     83 
     84 
     85     public String getName() {
     86         return name;
     87     }
     88 
     89     
     90     @Override
     91     public void accept(Visitor v) {
     92         v.visit(this);
     93     }}
     94 /*
     95  * 抽象访问者
     96  */
     97 interface Visitor{
     98     public void visit(Undergraduate stu);
     99     public void visit(GraduateStudent stu);
    100 }
    101 
    102 /*
    103  * 具体访问者
    104  */
    105 class Company implements Visitor{
    106 
    107     public void visit(Undergraduate stu) {
    108         if(stu.getMath() > 80 && stu.getEnglish() > 90)
    109             System.out.println(stu.getName() + "被录用");
    110     }
    111 
    112     public void visit(GraduateStudent stu) {
    113         if(stu.getMath() > 0 )
    114             System.out.println(stu.getName() + "被录用");
    115     }
    116     
    117 }

  • 相关阅读:
    《图解HTTP》简单的HTTP协议
    《图解HTTP》web及网络基础
    尚硅谷--雷丰阳--ElasticSearch 7.4.2版本 -- 有道云笔记
    ElasticSearch _bulk批量插入报错
    SpringBoot利用Filter来解决跨域问题
    js中数组的常用方法
    Mysql时间加减函数
    mybatis映射文件中不能使用">""<""&"问题
    博客园样式第二版
    GO学习笔记之 map
  • 原文地址:https://www.cnblogs.com/cxy2016/p/7670590.html
Copyright © 2011-2022 走看看