package org.ustc.visitor;
public abstract class Action {
public String name;
public Action(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract void getManConclusion(Man man);
abstract void getWomanConclusion(Woman woman);
}
package org.ustc.visitor;
public abstract class Person {
public String name;
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract void Accept(Action visitor);
}
package org.ustc.visitor;
public class Failing extends Action {
public Failing(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
void getManConclusion(Man man) {
// TODO Auto-generated method stub
System.out.println(man.getName()+this.getName()+"时,闷头喝酒,谁也不用劝");
}
@Override
void getWomanConclusion(Woman woman) {
// TODO Auto-generated method stub
System.out.println(woman.getName()+this.getName()+"时,眼泪汪汪,谁劝不了");
}
}
package org.ustc.visitor;
public class Success extends Action {
public Success(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
void getManConclusion(Man man) {
// TODO Auto-generated method stub
System.out.println(man.getName()+this.getName()+"时,背后多半有一个伟大的女人");
}
@Override
void getWomanConclusion(Woman woman) {
// TODO Auto-generated method stub
System.out.println(woman.getName()+this.getName()+"时多半有一个不成功的男人");
}
}
package org.ustc.visitor;
public class Man extends Person{
public Man(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void Accept(Action visitor) {
// TODO Auto-generated method stub
visitor.getManConclusion(this);
}
}
package org.ustc.visitor;
public class Woman extends Person {
public Woman(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void Accept(Action visitor) {
// TODO Auto-generated method stub
visitor.getWomanConclusion(this);
}
}
package org.ustc.visitor;
import java.util.ArrayList;
import java.util.List;
public class DisplayObject {
private List<Person> list;
public DisplayObject() {
list = new ArrayList<Person>();
}
public void attach(Person p){
list.add(p);
}
public void detach(Person p){
list.remove(p);
}
public void display(Action visitor){
for(Person p:list)
p.Accept(visitor);
}
}
package org.ustc.visitor;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DisplayObject obj = new DisplayObject();
obj.attach(new Man("男人"));
obj.attach(new Woman("女人"));
Success v1 = new Success("成功");
obj.display(v1);
Failing v2 = new Failing("失败");
obj.display(v2);
}
}
package org.ustc.visitor;public abstract class Action { public String name; public Action(String name) { super(); this.name = name; } public String getName() { return name; } public vo