用junit来测试代码
public class fwzTest {
@Test
public void Test1(){
VistorBei girl = new Girl();
VistorBei guy = new Guy();
China china = new China();
china.add(girl);
china.add(guy);
Vistor enemy = new Enemy();
Vistor friend = new Friend();
china.accept(enemy);
china.accept(friend);
}
}
public class China {
private List<VistorBei> vb = new ArrayList<>();
public void add(VistorBei vistorBei){
vb.add(vistorBei);
}
public void accept(Vistor vistor){
for (Object o : vb) {
((VistorBei) o).accept(vistor);
}
}
}
public interface Vistor {
void visitGril(Girl girl);
void visitGuy(Guy guy);
}
public interface VistorBei {
void accept(Vistor v);
}
public class Friend implements Vistor{
@Override
public void visitGril(Girl girl) {
girl.dance();
}
@Override
public void visitGuy(Guy guy) {
guy.handshake();
}
}
public class Enemy implements Vistor{
@Override
public void visitGril(Girl girl) {
girl.kill();
}
@Override
public void visitGuy(Guy guy) {
guy.kill();
}
}
public class Girl implements VistorBei{
@Override
public void accept(Vistor v) {
v.visitGril(this);
}
public void dance(){
System.out.println("跳舞~");
}
public void kill(){
System.out.println("砍你~");
}
}
public class Guy implements VistorBei{
@Override
public void accept(Vistor v) {
v.visitGuy(this);
}
public void handshake(){
System.out.println("握手~");
}
public void kill(){
System.out.println("拿烟头烫你~");
}
}