标签:board bsp imp nts http static class rri 键盘
(图:wiki)
public interface ComputerPart { void accept(Visitor computerVisitor); } public class KeyBoard implements ComputerPart { @Override public void accept(Visitor computerVisitor) { computerVisitor.visit(this); } } public class Mouse implements ComputerPart { @Override public void accept(Visitor computerVisitor) { computerVisitor.visit(this); } } public class Computer implements ComputerPart { private KeyBoard keyBoard= new KeyBoard(); private Mouse mouse = new Mouse(); @Override public void accept(Visitor computerVisitor) { computerVisitor.visit(this); keyBoard.accept(computerVisitor); mouse.accept(computerVisitor); } } public interface Visitor { void visit(Computer computer); void visit(Mouse mouse); void visit(KeyBoard keyBoard); } public class ConcreteVisitor implements Visitor { @Override public void visit(Computer computer) { System.out.println("访问电脑:"+computer); } @Override public void visit(Mouse mouse) { System.out.println("访问鼠标:"+mouse); } @Override public void visit(KeyBoard keyBoard) { System.out.println("访问键盘:"+keyBoard); } } //测试 public class Client { public static void main(String[] args){ Computer computer = new Computer(); Visitor visitor = new ConcreteVisitor(); computer.accept(visitor); } }
标签:board bsp imp nts http static class rri 键盘
原文地址:https://www.cnblogs.com/camcay/p/12380576.html