File tree Expand file tree Collapse file tree 9 files changed +135
-0
lines changed
src/com/anxpp/designpattern/visitor Expand file tree Collapse file tree 9 files changed +135
-0
lines changed Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+ //具体访问者
3+ public class APPOwner implements Visitor {
4+ @ Override
5+ public void visit (UserVIP user ){
6+ String estimation = user .getEstimation ();
7+ if (estimation .length ()>5 )
8+ System .out .println ("记录一条有效反馈:" + estimation );
9+ }
10+ @ Override
11+ public void visit (UserOrdinary user ){
12+ String estimation = user .getEstimation ();
13+ if (estimation .length ()>10 )
14+ System .out .println ("记录一条有效反馈:" + estimation );
15+ }
16+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+ //演示java的静态分派和动态分派
3+ //结果会输出:Collection
4+ //所以重载的分派是根据静态类型进行的
5+ public class Dispatch {
6+ void print (FatherClass c ){
7+ System .out .print ("父类" );
8+ }
9+ void print (ChildClass c ){
10+ System .out .print ("子类" );
11+ }
12+ public static void main (String args []){
13+ FatherClass child = new ChildClass ();
14+ new Dispatch ().print (child );
15+ child .print ();
16+ }
17+ }
18+ class FatherClass {
19+ void print (){
20+ System .out .println ("父类" );
21+ }
22+ }
23+ class ChildClass extends FatherClass {
24+ void print (){
25+ System .out .print ("子类" );
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+
3+ public class MultiDispatch {
4+ public static void main (String args []){
5+ Father child = new Child ();
6+ child .print ();
7+ new Child ().print (new Vistor ());
8+ }
9+ }
10+ class Father {
11+ void print (){
12+ System .out .println ("父类" );
13+ }
14+ }
15+ class Child extends Father {
16+ void print (){
17+ System .out .print ("子类" );
18+ }
19+ void print (Vistor c ){
20+ c .print (this );
21+ }
22+ }
23+ class Vistor {
24+ public void print (Child child ){
25+ child .print ();
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Iterator ;
5+
6+ public class TestUse {
7+ public static void main (String args []){
8+ Visitor appOwner = new APPOwner ();
9+ ArrayList <User > users = new ArrayList <User >();
10+ users .add (new UserOrdinary ("普通用户短反馈" ));
11+ users .add (new UserOrdinary ("这是一个普通用户的比较长的反馈" ));
12+ users .add (new UserVIP ("VIP用户的短反馈" ));
13+ users .add (new UserVIP ("VIP用户的比较长的反馈反馈" ));
14+ Iterator <User > iterator = users .iterator ();
15+ while (iterator .hasNext ()){
16+ iterator .next ().accept (appOwner );
17+ }
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+ //抽象元素
3+ public interface User {
4+ void accept (Visitor visitor );
5+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+ //普通用户,具体元素
3+ public class UserOrdinary implements User {
4+ String estimation ;
5+ public UserOrdinary (String estimation ){
6+ this .estimation = estimation ;
7+ }
8+ @ Override
9+ public void accept (Visitor visitor ){
10+ visitor .visit (this );
11+ }
12+ String getEstimation (){
13+ return estimation ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+ //VIP用户,具体元素
3+ public class UserVIP implements User {
4+ String estimation ;
5+ public UserVIP (String estimation ){
6+ this .estimation = estimation ;
7+ }
8+ @ Override
9+ public void accept (Visitor visitor ){
10+ visitor .visit (this );
11+ }
12+ String getEstimation (){
13+ return estimation ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .anxpp .designpattern .visitor ;
2+ //抽象访问者
3+ public interface Visitor {
4+ void visit (UserVIP user );
5+ void visit (UserOrdinary user );
6+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * @author Administrator
3+ * 访问者模式
4+ */
5+ package com .anxpp .designpattern .visitor ;
You can’t perform that action at this time.
0 commit comments