Tutorial 4
Visitor Pattern
   Wei Wang
 Nov. 25th, 2011
Goals
• Scenario of using Visitor Pattern

• Why naï approaches are bad?
        ve

• Elements of Visitor Pattern

• Real world application

                                      2
Scenario
• Sarah wants to find out who does NOT like
  Lady Gaga at UWaterloo




                                              3
• For male (female) students: borrowed >1
  (>3)book about Lady Gaga from school library

• For profs: purchased >2 Lady Gaga CDs

• For staff: …



                                             4
Essence of this scenario
• Iterate a variety of elements under one
  hierarchy
  – Student/prof/staff
• Customization of the iterating algorithm
  – Gender/library history/purchasing history
• Data aggregation
  – Total number of student fans? What about CS only?
• New algorithm may arise from future demands
  – Looking for fans of Justin Bieber?
• “open for extension, but closed for modification”

                                                        5
Naï solution 1
                ve
isLadyGagaFans(IPerson){

If(Iperson instanceof
Student){
}                             Instanceof and type cast!

Else if (Iperson instanceof
Professor){

    }
}


                                                          6
Naï Solution 2
                   ve
class Student{



    isLadyGagaFans(){         Algorithm defined in the student class

     checklibraryRecords();

     checkGender();

     }
}
                                                                       7
Problems of Naï solutions
                 ve
• “instanceof”        •“pseudo OO” solution
  solution:
                        •Touch original code
  – Instanceof or
    type casting is
                        •Similar solution is
    error prone
                        scattered in many
  – hard coding!        place!




                                               8
Visitor Pattern Solution
Class Student implement IPerson{
  void accept(Visitor visitor){
    visitor.visit(this);
  }
}

Class LadyGagaFansChecker(){
   visit(Student student){
          student.checkLibraryHistory();
          student.checkGender();
          print;
}
}

…… studentA.accept(new LadyGagaFansChecker());

                                                 9
Class Diagram of Visitor Pattern




                   CheckJustinBieberFa
                            n




                                         10
Real world application
• Processing syntactical elements in compiler
  design
  – Eclipse JDT




                                                11
Visitor Pattern in JDT

compiler         ASTVisitor




                DeclarationVisitor
                                         MethodVisitor




                           If the syntax of Java changes
                         (such as generics and enhanced
                           loop), we just need add new
                          visitors and invoke them later
                                                           12
What if we cannot change JDT?
• The Compiler of AspectJ reuses most of JDT
  elements (because we still need to compile
  the JAVA part of the code)

  AspectJ
  compiler



                          JointPointVisitor




             JointPoint

                                               13
14
Take-away of this tutorial
• Separate the algorithm with from an object
  structure on which it operates
• Easy to add new operations on existing
  objects
  – “open for extension, but closed for modification”
• Almost all compilers implementation use
  visitor pattern to iterate syntactical elements
  – JDT & AspectJ compiler

                                                    15

Tutorial visitor

  • 1.
    Tutorial 4 Visitor Pattern Wei Wang Nov. 25th, 2011
  • 2.
    Goals • Scenario ofusing Visitor Pattern • Why naï approaches are bad? ve • Elements of Visitor Pattern • Real world application 2
  • 3.
    Scenario • Sarah wantsto find out who does NOT like Lady Gaga at UWaterloo 3
  • 4.
    • For male(female) students: borrowed >1 (>3)book about Lady Gaga from school library • For profs: purchased >2 Lady Gaga CDs • For staff: … 4
  • 5.
    Essence of thisscenario • Iterate a variety of elements under one hierarchy – Student/prof/staff • Customization of the iterating algorithm – Gender/library history/purchasing history • Data aggregation – Total number of student fans? What about CS only? • New algorithm may arise from future demands – Looking for fans of Justin Bieber? • “open for extension, but closed for modification” 5
  • 6.
    Naï solution 1 ve isLadyGagaFans(IPerson){ If(Iperson instanceof Student){ } Instanceof and type cast! Else if (Iperson instanceof Professor){ } } 6
  • 7.
    Naï Solution 2 ve class Student{ isLadyGagaFans(){ Algorithm defined in the student class checklibraryRecords(); checkGender(); } } 7
  • 8.
    Problems of Naïsolutions ve • “instanceof” •“pseudo OO” solution solution: •Touch original code – Instanceof or type casting is •Similar solution is error prone scattered in many – hard coding! place! 8
  • 9.
    Visitor Pattern Solution ClassStudent implement IPerson{ void accept(Visitor visitor){ visitor.visit(this); } } Class LadyGagaFansChecker(){ visit(Student student){ student.checkLibraryHistory(); student.checkGender(); print; } } …… studentA.accept(new LadyGagaFansChecker()); 9
  • 10.
    Class Diagram ofVisitor Pattern CheckJustinBieberFa n 10
  • 11.
    Real world application •Processing syntactical elements in compiler design – Eclipse JDT 11
  • 12.
    Visitor Pattern inJDT compiler ASTVisitor DeclarationVisitor MethodVisitor If the syntax of Java changes (such as generics and enhanced loop), we just need add new visitors and invoke them later 12
  • 13.
    What if wecannot change JDT? • The Compiler of AspectJ reuses most of JDT elements (because we still need to compile the JAVA part of the code) AspectJ compiler JointPointVisitor JointPoint 13
  • 14.
  • 15.
    Take-away of thistutorial • Separate the algorithm with from an object structure on which it operates • Easy to add new operations on existing objects – “open for extension, but closed for modification” • Almost all compilers implementation use visitor pattern to iterate syntactical elements – JDT & AspectJ compiler 15