场景:男人和女人,玩游戏时有不同的状态。
首先设计一个抽象人类,再设计一个男人和女人类继承该抽象类;这两个类当做被访问的元素;
再设计一个访问者接口,和一个赢游戏时状态和输游戏时状态实现该接口;
接着是一个管理被访问元素的类,最后是客户端主类。
抽象类和被访问元素类:
package com.freshbin.pattern.visitor.myexample.element;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;
/**
* Person抽象类
*
* @author freshbin
* @date 2019年1月27日 下午4:00:55
*/
public abstract class Person {
private String type;
public Person(String type) {
this.type = type;
}
public abstract void accept(StateInterface stateInterface);
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
package com.freshbin.pattern.visitor.myexample.element;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;
/**
* 男人类
*
* @author freshbin
* @date 2019年1月27日 下午4:06:34
*/
public class Man extends Person {
public Man() {
super("男人");
}
@Override
public void accept(StateInterface stateInterface) {
stateInterface.visitPerson(this);
}
}
package com.freshbin.pattern.visitor.myexample.element;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;
/**
* 女人类
*
* @author freshbin
* @date 2019年1月27日 下午4:06:34
*/
public class Woman extends Person {
public Woman() {
super("女人");
}
@Override
public void accept(StateInterface stateInterface) {
stateInterface.visitPerson(this);
}
}
访问者接口和具体类:
package com.freshbin.pattern.visitor.myexample.visitor;
import com.freshbin.pattern.visitor.myexample.element.Person;
/**
* 状态接口
*
* @author freshbin
* @date 2019年1月27日 下午4:08:16
*/
public interface StateInterface {
public void visitPerson(Person person);
}
package com.freshbin.pattern.visitor.myexample.visitor;
import com.freshbin.pattern.visitor.myexample.element.Person;
/**
* WinGame
*
* @author freshbin
* @date 2019年1月27日 下午4:13:15
*/
public class WinGame implements StateInterface {
@Override
public void visitPerson(Person person) {
switch (person.getType()) {
case "男人":
System.out.println("打游戏时赢了:" + person.getType() + "会淡定!");
break;
case "女人":
System.out.println("打游戏时赢了:" + person.getType() + "会欣喜!");
break;
default:
break;
}
}
}
package com.freshbin.pattern.visitor.myexample.visitor;
import com.freshbin.pattern.visitor.myexample.element.Person;
/**
* LoseGame
*
* @author freshbin
* @date 2019年1月27日 下午4:13:15
*/
public class LoseGame implements StateInterface {
@Override
public void visitPerson(Person person) {
switch (person.getType()) {
case "男人":
System.out.println("打游戏时输了:" + person.getType() + "会淡定!");
break;
case "女人":
System.out.println("打游戏时输了:" + person.getType() + "会伤心!");
break;
default:
break;
}
}
}
管理被访问元素的类:
package com.freshbin.pattern.visitor.myexample;
import java.util.LinkedList;
import com.freshbin.pattern.visitor.myexample.element.Person;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;
public class ObjectStructure {
private LinkedList<Person> element = new LinkedList<>();
public void addElement(Person person) {
element.add(person);
}
public void display(StateInterface stateInterface) {
element.parallelStream().forEach((person) -> {
person.accept(stateInterface);
});
}
}
客户端主类:
package com.freshbin.pattern.visitor.myexample;
import com.freshbin.pattern.visitor.myexample.element.Man;
import com.freshbin.pattern.visitor.myexample.element.Person;
import com.freshbin.pattern.visitor.myexample.element.Woman;
import com.freshbin.pattern.visitor.myexample.visitor.LoseGame;
import com.freshbin.pattern.visitor.myexample.visitor.StateInterface;
import com.freshbin.pattern.visitor.myexample.visitor.WinGame;
/**
* 访问者模式
*
* @author freshbin
* @date 2019年1月27日 下午4:18:27
*/
public class VisitorPatternDemo {
public static void main(String[] args) {
Person man = new Man();
Person woman = new Woman();
StateInterface winGame = new WinGame();
StateInterface loseGame = new LoseGame();
ObjectStructure objectStructure = new ObjectStructure();
objectStructure.addElement(man);
objectStructure.addElement(woman);
objectStructure.display(winGame);
System.out.println("=======================================");
objectStructure.display(loseGame);
}
}
效果图: