Skip to content

Commit fb6bf03

Browse files
committed
访问者模式
1 parent ceff7c0 commit fb6bf03

File tree

9 files changed

+135
-0
lines changed

9 files changed

+135
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
//具体访问者
3+
publicclassAPPOwnerimplementsVisitor{
4+
@Override
5+
publicvoidvisit(UserVIPuser){
6+
Stringestimation = user.getEstimation();
7+
if(estimation.length()>5)
8+
System.out.println("记录一条有效反馈:" + estimation);
9+
}
10+
@Override
11+
publicvoidvisit(UserOrdinaryuser){
12+
Stringestimation = user.getEstimation();
13+
if(estimation.length()>10)
14+
System.out.println("记录一条有效反馈:" + estimation);
15+
}
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
//演示java的静态分派和动态分派
3+
//结果会输出:Collection
4+
//所以重载的分派是根据静态类型进行的
5+
publicclassDispatch{
6+
voidprint(FatherClassc){
7+
System.out.print("父类");
8+
}
9+
voidprint(ChildClassc){
10+
System.out.print("子类");
11+
}
12+
publicstaticvoidmain(Stringargs[]){
13+
FatherClasschild = newChildClass();
14+
newDispatch().print(child);
15+
child.print();
16+
}
17+
}
18+
classFatherClass{
19+
voidprint(){
20+
System.out.println("父类");
21+
}
22+
}
23+
classChildClassextendsFatherClass{
24+
voidprint(){
25+
System.out.print("子类");
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
3+
publicclassMultiDispatch{
4+
publicstaticvoidmain(Stringargs[]){
5+
Fatherchild = newChild();
6+
child.print();
7+
newChild().print(newVistor());
8+
}
9+
}
10+
classFather{
11+
voidprint(){
12+
System.out.println("父类");
13+
}
14+
}
15+
classChildextendsFather{
16+
voidprint(){
17+
System.out.print("子类");
18+
}
19+
voidprint(Vistorc){
20+
c.print(this);
21+
}
22+
}
23+
classVistor{
24+
publicvoidprint(Childchild){
25+
child.print();
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Iterator;
5+
6+
publicclassTestUse{
7+
publicstaticvoidmain(Stringargs[]){
8+
VisitorappOwner = newAPPOwner();
9+
ArrayList<User> users = newArrayList<User>();
10+
users.add(newUserOrdinary("普通用户短反馈"));
11+
users.add(newUserOrdinary("这是一个普通用户的比较长的反馈"));
12+
users.add(newUserVIP("VIP用户的短反馈"));
13+
users.add(newUserVIP("VIP用户的比较长的反馈反馈"));
14+
Iterator<User> iterator = users.iterator();
15+
while(iterator.hasNext()){
16+
iterator.next().accept(appOwner);
17+
}
18+
}
19+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
//抽象元素
3+
publicinterfaceUser{
4+
voidaccept(Visitorvisitor);
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
//普通用户,具体元素
3+
publicclassUserOrdinaryimplementsUser{
4+
Stringestimation;
5+
publicUserOrdinary(Stringestimation){
6+
this.estimation = estimation;
7+
}
8+
@Override
9+
publicvoidaccept(Visitorvisitor){
10+
visitor.visit(this);
11+
}
12+
StringgetEstimation(){
13+
returnestimation;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
//VIP用户,具体元素
3+
publicclassUserVIPimplementsUser{
4+
Stringestimation;
5+
publicUserVIP(Stringestimation){
6+
this.estimation = estimation;
7+
}
8+
@Override
9+
publicvoidaccept(Visitorvisitor){
10+
visitor.visit(this);
11+
}
12+
StringgetEstimation(){
13+
returnestimation;
14+
}
15+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagecom.anxpp.designpattern.visitor;
2+
//抽象访问者
3+
publicinterfaceVisitor{
4+
voidvisit(UserVIPuser);
5+
voidvisit(UserOrdinaryuser);
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @author Administrator
3+
* 访问者模式
4+
*/
5+
packagecom.anxpp.designpattern.visitor;

0 commit comments

Comments
(0)