行为设计模式-模板方法(Visitor)

行为设计模式-模板方法(Visitor)_第1张图片

package com.leitao.demo.design;

import java.util.ArrayList;
import java.util.List;

/**
 * 访问者
 * @Description: TODO
 * @Author LeiTao
 * @Date 2023/5/5
 * @Version V1.0
 **/
public class VisitorPattern {
    public static void main(String[] args) {
        PersonStructure personStructure = new PersonStructure();
        VisitorA visitorA = new VisitorA();
        System.out.println("访问者A的访问记录");
        personStructure.accept(visitorA);
        System.out.println("学生年龄的总和:" + visitorA.getStudentAgeSum());
        System.out.println("老师年龄的总和:" + visitorA.getTeacherAgeSum());
        System.out.println("===============");
        VisitorB visitorB = new VisitorB();
        System.out.println("访问者B的访问记录");
        personStructure.accept(visitorB);
        System.out.println("学生最高的成绩:" + visitorB.getMaxScore());
        System.out.println("老师最高的工龄:" + visitorB.getMaxWorkYear());
    }
}

abstract class Visitor{
    public abstract void visitStudent(Student student);
    public abstract void visitTeacher(Teacher teacher);
}

class VisitorA extends Visitor {
    private Integer studentAgeSum = 0;
    private Integer teacherAgeSum = 0;

    public Integer getStudentAgeSum() {
        return studentAgeSum;
    }

    public Integer getTeacherAgeSum() {
        return teacherAgeSum;
    }

    @Override
    public void visitStudent(Student student) {
        System.out.println("访问者A学生的姓名:" + student.getName()+ "成绩:" + student.getScore());
        studentAgeSum += student.getAge();
    }

    @Override
    public void visitTeacher(Teacher teacher) {
        System.out.println("访问者A老师的姓名:" + teacher.getName() + "工龄" + teacher.getWorkYear());
        teacherAgeSum += teacher.getAge();
    }
}

class VisitorB extends Visitor {
    private Double maxScore = -1.0;
    private Integer maxWorkYear = -1;

    public Double getMaxScore() {
        return maxScore;
    }

    public Integer getMaxWorkYear() {
        return maxWorkYear;
    }

    @Override
    public void visitStudent(Student student) {
        System.out.println("访问者B学生的姓名:" + student.getName()+ "成绩:" + student.getScore());
        maxScore = Math.max(maxScore,student.getScore());
    }

    @Override
    public void visitTeacher(Teacher teacher) {
        System.out.println("访问者B老师的姓名:" + teacher.getName() + "工龄" + teacher.getWorkYear());
        maxWorkYear = Math.max(maxWorkYear,teacher.getWorkYear());
    }
}

class PersonStructure{
    private List personList = new ArrayList();

    public PersonStructure() {
        personList.add(new Student("张胜男",23,34.23));
        personList.add(new Student("李斯",454,23));
        personList.add(new Student("王武",56,22.21));
        personList.add(new Teacher("刘老师",12,45));
        personList.add(new Teacher("吴老四",34,6));
        personList.add(new Teacher("崔老师",13,1));
    }
    public void accept(Visitor visitor) {
        for (Person person : personList) {
            person.accept(visitor);
        }
    }
}

abstract class Person{
    private String name;
    private Integer age;

    public Person(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public abstract void accept(Visitor visitor);
}

class Student extends Person {
    private double score;
    public Student(String name,Integer age,double score) {
        super(name,age);
        this.score = score;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visitStudent(this);
    }
}

class Teacher extends Person{
    private Integer workYear;

    public Teacher(String name,Integer age,Integer workYear) {
        super(name, age);
        this.workYear = workYear;
    }

    public Integer getWorkYear() {
        return workYear;
    }

    public void setWorkYear(Integer workYear) {
        this.workYear = workYear;
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visitTeacher(this);
    }
}

 行为设计模式-模板方法(Visitor)_第2张图片

 

你可能感兴趣的:(设计模式,java,jvm)