美文网首页
设计模式(24)之访问者模式

设计模式(24)之访问者模式

作者: zhglance | 来源:发表于2019-07-14 22:16 被阅读0次

访问者模式(Visitor Pattern)提供了Visitor类,作用于某对象的各个类型(子类)的操作,实现了在不改变这些类型的情况下,可以生成对这些类型的新操作。

一、需求举例:

访问者类访问物理机,虚拟机和Docker。

二、类图:

三、代码实例:

1.机器父类:Machine

package com.lance.visitor;

public interface Machine {
    void accept(MachineVisitor machineVisitor);
}

2.机器访问者类:MachineVisitor

package com.lance.visitor;

public interface MachineVisitor {
    void visit(PhysicalMachine physicalMachine);
    void visit(VirtualMachine virtualMachine);
    void visit(DockerMachine dockerMachine);
}

3.物理机类:PhysicalMachine

package com.lance.visitor;

public class PhysicalMachine  implements Machine {
    @Override
    public void accept(MachineVisitor machineVisitor) {
        machineVisitor.visit(this);
    }
}

4.虚拟机类:

package com.lance.visitor;

public class VirtualMachine  implements Machine {
    @Override
    public void accept(MachineVisitor machineVisitor) {
        machineVisitor.visit(this);
    }
}

5.Docker机器类:

package com.lance.visitor;

public class DockerMachine implements Machine {
    @Override
    public void accept(MachineVisitor machineVisitor) {
        machineVisitor.visit(this);
    }
}

6.机器访问者实现类:MachineVisitorImpl

package com.lance.visitor;

public class MachineVisitorImpl implements MachineVisitor {
    @Override
    public void visit(PhysicalMachine physicalMachine) {
        System.out.println("visit physical machine.");
    }

    @Override
    public void visit(VirtualMachine virtualMachine) {
        System.out.println("visit virtual machine.");
    }

    @Override
    public void visit(DockerMachine dockerMachine) {
        System.out.println("visit docker machine.");
    }
}

7.机器访问者实现类2:

package com.lance.visitor;


public class MachineVisitorImpl2 implements MachineVisitor {
    @Override
    public void visit(PhysicalMachine physicalMachine) {
        System.out.println("visit physical machine[2].");
    }

    @Override
    public void visit(VirtualMachine virtualMachine) {
        System.out.println("visit virtual machine[2].");
    }

    @Override
    public void visit(DockerMachine dockerMachine) {
        System.out.println("visit docker machine[2].");
    }
}

8.机器结构类:

package com.lance.visitor;

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

public class MachineStructure {

    private List<Machine> machines = new ArrayList<>();

    public void addMachine(Machine machine) {
        machines.add(machine);
    }

    public void accept(MachineVisitor machineVisitor) {
        for(Machine machine:machines) {
            machine.accept(machineVisitor);
        }
    }

}

9.主类:VisitorPattern

package com.lance.visitor;

public class VisitorPattern {

    public static void main(String[] args) {
        System.out.println("==========start==========");


        MachineStructure machineStructure = new MachineStructure();
        machineStructure.addMachine(new PhysicalMachine());
        machineStructure.addMachine(new VirtualMachine());
        machineStructure.addMachine(new DockerMachine());

        MachineVisitor machineVisitor = new MachineVisitorImpl();

        machineStructure.accept(machineVisitor);

        System.out.println("=======================");

        MachineStructure machineStructure2 = new MachineStructure();
        machineStructure2.addMachine(new PhysicalMachine());
        machineStructure2.addMachine(new VirtualMachine());
        machineStructure2.addMachine(new DockerMachine());

        MachineVisitor machineVisitor2 = new MachineVisitorImpl2();

        machineStructure2.accept(machineVisitor2);


        System.out.println("==========end==========");

    }
}

输出结果:

==========start==========
visit physical machine.
visit virtual machine.
visit docker machine.
=======================
visit physical machine[2].
visit virtual machine[2].
visit docker machine[2].
==========end==========

相关文章

网友评论

      本文标题:设计模式(24)之访问者模式

      本文链接:https://www.haomeiwen.com/subject/xzcwhctx.html