File tree Expand file tree Collapse file tree 8 files changed +112
-90
lines changed
src/com/anxpp/designpattern/visitor Expand file tree Collapse file tree 8 files changed +112
-90
lines changed Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
2+
23//具体访问者
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- }
4+ public class APPOwner implements Visitor {
5+ @ Override
6+ public void visit (UserVIP user ) {
7+ String estimation = user .getEstimation ();
8+ if (estimation .length () > 5 )
9+ System .out .println ("记录一条有效反馈:" + estimation );
10+ }
11+
12+ @ Override
13+ public void visit (UserOrdinary user ) {
14+ String estimation = user .getEstimation ();
15+ if (estimation .length () > 10 )
16+ System .out .println ("记录一条有效反馈:" + estimation );
17+ }
1618}
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
2+
23//演示java的静态分派和动态分派
34//结果会输出:Collection
45//所以重载的分派是根据静态类型进行的
56public 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- }
7+ public static void main (String args []) {
8+ FatherClass child = new ChildClass ();
9+ new Dispatch ().print (child );
10+ child .print ();
11+ }
12+
13+ void print (FatherClass c ) {
14+ System .out .print ("父类" );
15+ }
16+
17+ void print (ChildClass c ) {
18+ System .out .print ("子类" );
19+ }
1720}
18- class FatherClass {
19- void print (){
20- System .out .println ("父类" );
21- }
21+
22+ class FatherClass {
23+ void print () {
24+ System .out .println ("父类" );
25+ }
2226}
23- class ChildClass extends FatherClass {
24- void print (){
25- System .out .print ("子类" );
26- }
27+
28+ class ChildClass extends FatherClass {
29+ void print () {
30+ System .out .print ("子类" );
31+ }
2732}
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
22
33public class MultiDispatch {
4- public static void main (String args []){
5- Father child = new Child ();
6- child .print ();
7- new Child ().print (new Vistor ());
8- }
4+ public static void main (String args []) {
5+ Father child = new Child ();
6+ child .print ();
7+ new Child ().print (new Vistor ());
8+ }
99}
10- class Father {
11- void print (){
12- System .out .println ("父类" );
13- }
10+
11+ class Father {
12+ void print () {
13+ System .out .println ("父类" );
14+ }
1415}
15- class Child extends Father {
16- void print (){
17- System .out .print ("子类" );
18- }
19- void print (Vistor c ){
20- c .print (this );
21- }
16+
17+ class Child extends Father {
18+ void print () {
19+ System .out .print ("子类" );
20+ }
21+
22+ void print (Vistor c ) {
23+ c .print (this );
24+ }
2225}
23- class Vistor {
24- public void print (Child child ){
25- child .print ();
26+
27+ class Vistor {
28+ public void print (Child child ) {
29+ child .print ();
2630 }
2731}
Original file line number Diff line number Diff line change 44import java .util .Iterator ;
55
66public 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- }
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+ }
1919}
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
2+
23//抽象元素
34public interface User {
4- void accept (Visitor visitor );
5+ void accept (Visitor visitor );
56}
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
2+
23//普通用户,具体元素
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- }
4+ public class UserOrdinary implements User {
5+ String estimation ;
6+
7+ public UserOrdinary (String estimation ) {
8+ this .estimation = estimation ;
9+ }
10+
11+ @ Override
12+ public void accept (Visitor visitor ) {
13+ visitor .visit (this );
14+ }
15+
16+ String getEstimation () {
17+ return estimation ;
18+ }
1519}
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
2+
23//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- }
4+ public class UserVIP implements User {
5+ String estimation ;
6+
7+ public UserVIP (String estimation ) {
8+ this .estimation = estimation ;
9+ }
10+
11+ @ Override
12+ public void accept (Visitor visitor ) {
13+ visitor .visit (this );
14+ }
15+
16+ String getEstimation () {
17+ return estimation ;
18+ }
1519}
Original file line number Diff line number Diff line change 11package com .anxpp .designpattern .visitor ;
2+
23//抽象访问者
34public interface Visitor {
4- void visit (UserVIP user );
5- void visit (UserOrdinary user );
5+ void visit (UserVIP user );
6+
7+ void visit (UserOrdinary user );
68}
You can’t perform that action at this time.
0 commit comments