设计模式之访问者模式

访问者模式

  1. 访问者模式结构图:

  2. 示例代码:

    // 抽象访问者
    public interface Visistor {
    
        void checkWork(Engineer engineer);
    
        void checkWork(Manager manager);
    }
    
    // 具体访问者boss
    public class BossVisistor implements Visistor{
        @Override
        public void checkWork(Engineer engineer) {
            engineer.workProgress();
        }
    
        @Override
        public void checkWork(Manager manager) {
            manager.workProgress();
        }
    }
    
    // 具体访问者投资人
    public class InvestorsVisistor implements Visistor{
        @Override
        public void checkWork(Engineer engineer) {
            engineer.workProgress();
        }
    
        @Override
        public void checkWork(Manager manager) {
            manager.workProgress();
        }
    }
    
    
    // 抽象工作者(假定员工只有工程师和项目经理)
    public abstract class Worker {
        protected String name;
        abstract void accept(Visistor visistor);
    }
    
    // 具体工作者工程师
    public class Engineer extends Worker{
    
        public Engineer(String name) {
            this.name = name;
        }
    
        @Override
        void accept(Visistor visistor) {
            visistor.checkWork(this);
        }
    
        public void workProgress(){
            System.out.println(this.getClass().getSimpleName() + name + "完成工作进度:" + new Random().nextInt(100) + "%");
        }
    }
    
    // 具体工作者项目经理
    public class Manager extends Worker{
    
        public Manager(String name) {
            this.name = name;
        }
    
        @Override
        void accept(Visistor visistor) {
            visistor.checkWork(this);
        }
    
        public void workProgress(){
            System.out.println(this.getClass().getSimpleName() + name + "完成产品设计进度:" + new Random().nextInt(100) + "%");
        }
    }
    
    // 容器
    public class ObjectStruct {
        List<Worker> list = new ArrayList<>();
    
        public void accept(Visistor visistor){
            list.forEach(a -> a.accept(visistor));
        }
    
        public void add(Worker worker){
            list.add(worker);
        }
    
        public void remove(Worker worker){
            list.remove(worker);
        }
    }
    
    // 测试
    public class VisistorTest {
    
        public static void main(String[] args) {
            ObjectStruct objectStruct = new ObjectStruct();
            objectStruct.add(new Engineer("张三"));
            objectStruct.add(new Engineer("李四"));
            objectStruct.add(new Engineer("王五"));
            objectStruct.add(new Manager("斯蒂芬"));
            objectStruct.add(new Manager("詹姆斯"));
            objectStruct.add(new Manager("约翰逊"));
    
            System.out.println("=========老板检查工作进度===========");
            objectStruct.accept(new BossVisistor());
            System.out.println("=========投资人检查工作进度===========");
            objectStruct.accept(new InvestorsVisistor());
        }
    }
    
    
  3. 总结:(举例不是太恰当,主要是为了学习访问者模式)

    优点:解偶数据结构与数据操作,符合单一职责原则;

    缺点:无法增加元素类型,违背依赖倒置原则.

posted @ 2021-04-21 23:39  justKen  阅读(57)  评论(0编辑  收藏  举报