skip to main content
10.1145/1449764.1449799acmconferencesArticle/Chapter ViewAbstractPublication PagessplashConference Proceedingsconference-collections
research-article

The visitor pattern as a reusable, generic, type-safe component

Published:19 October 2008Publication History

ABSTRACT

The VISITOR design pattern shows how to separate the structure of an object hierarchy from the behaviour of traversals over that hierarchy. The pattern is very flexible; this very flexibility makes it difficult to capture the pattern as anything more formal than prose, pictures and prototypes.

We show how to capture the essence of the VISITOR pattern as a reusable software library, by using advanced type system features appearing in modern object-oriented languages such as Scala. We preserve type-safety statically and modularly: no reflection or similar mechanisms are used and modules can be independently compiled. The library is generic, in two senses: not only is it parametrised by both the return type and the shape of the object hierarchy, but also it allows a number of implementation choices (internal versus external control, imperative versus functional behaviour, orthogonal aspects such as tracing and memoisation) to be specified by parameters rather than fixed in early design decisions. Finally, we propose a generalised datatype-like notation,on top of our visitor library: this provides a convenient functional decomposition style in object-oriented languages.

References

  1. Jonathan Aldrich. Open modules: Modular reasoning about advice. In LNCS 3586: European Conference on Object-Oriented Programming, pages 144--168, 2005. Google ScholarGoogle Scholar
  2. Karine Arnout. Pattern Componentization. PhD thesis, Swiss Institute of Technology, March 2004.Google ScholarGoogle Scholar
  3. Richard Bird and Lambert Meertens. Nested datatypes. In J. Jeuring, editor, LNCS 1422: Mathematics of Program Construction, pages 52--67, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  4. Richard S. Bird and Oege De Moor. Algebra of Programming. Prentice Hall, 1997. Google ScholarGoogle ScholarDigital LibraryDigital Library
  5. Corrado Bohm and Alesssandro Berarducci. Automatic synthesis of typed lambda-programs on term algebras. Theoretical Computer Science, 39(2-3):135--153, August 1985.Google ScholarGoogle ScholarCross RefCross Ref
  6. Peter Buchlovsky and Hayo Thielecke. A type-theoretic reconstruction of the Visitor pattern. Electronic Notes in Theoretical Computer Science, 155, 2005. Mathematical Foundations of Programming Semantics. Google ScholarGoogle ScholarDigital LibraryDigital Library
  7. Bryan Chadwick and Karl Lieberherr. Functional Adaptive Programming with DemeterF. Technical Report NU-CCIS-08-March19, Northeastern University, Boston, March 2008.Google ScholarGoogle Scholar
  8. Bryan Chadwick, Therapon Skotiniotis, and Karl Lieberherr. Functional Visitors Revisited. Technical Report NU-CCIS-06-03, Northeastern University, Boston, May 2006.Google ScholarGoogle Scholar
  9. Craig Chambers and Gary T. Leavens. Typechecking and modules for multimethods. ACM Transactions on Programming Languages and Systems, 17(6):805--843, November 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  10. Alonzo Church. An unsolvable problem of elementary number theory. American Journal of Mathematics, 58:345--363, 1936.Google ScholarGoogle Scholar
  11. Curtis Clifton, Gary T. Leavens, Craig Chambers, and Todd Millstein. MultiJava: Modular open classes and symmetric multiple dispatch for Java. In Object-Oriented Programming, Systems, Languages, and Applications, pages 130--145, 2000. Google ScholarGoogle Scholar
  12. Vincent Cremet and Philippe Altherr. Adding type constructor parameterization to Java. Journal of Object Technology, 7(5):25--65, June 2008. Special issue on ECOOP 2007 Workshop on Formal Techniques for Java-like Programs.Google ScholarGoogle ScholarCross RefCross Ref
  13. Antonio Cunei and Jan Vitek. PolyD: a flexible dispatching framework. In Object Oriented Programming, Systems, Languages, and Applications, pages 487--503, New York, NY, USA, 2005. ACM. doi: http://doi.acm.org/10.1145/ 1103845.1094849. Google ScholarGoogle Scholar
  14. Michael Ernst, Craig Kaplan, and Craig Chambers. Predicate dispatching: A unified theory of dispatch. In European Conference on Object-Oriented Programming, pages 186--211, London, UK, 1998. Springer-Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  15. Remi Forax, Etienne Duris, and Gilles Roussel. Reflection-based implementation of Java extensions: The double-dispatch use-case. In ACM Symposium on Applied Computing, pages 1409--1413, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  16. Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  17. Jeremy Gibbons. Patterns in datatype-generic programming. In J. Striegnitz, editor, Declarative Programming in the Context of Object-Oriented Languages, Uppsala, 2003.Google ScholarGoogle Scholar
  18. Jeremy Gibbons. Design patterns as higher-order datatype-generic programs. In ACM Workshop on Generic Programming, pages 1--12, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  19. Jeremy Gibbons and Bruno Oliveira. The essence of the Iterator pattern. Journal of Functional Programming, 2008. Google ScholarGoogle ScholarDigital LibraryDigital Library
  20. Jean-Yves Girard, Paul Taylor, and Yves Lafont. Proofs and types. Cambridge University Press, 1989. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. Christian Grothoff. Walkabout revisited: The Runabout. In LNCS 2743: European Conference on Object-Oriented Programming, pages 103--125, 2003.Google ScholarGoogle Scholar
  22. Stephan Gudmundson and Gregor Kiczales. Addressing practical software development issues in AspectJ with a pointcut interface. In ECOOP 2001 Workshop on Advanced Separation of Concerns, 2001.Google ScholarGoogle Scholar
  23. Jan Hannemann and Gregor Kiczales. Design pattern implementation in Java and AspectJ. SIGPLAN Not., 37(11): 161--173, 2002. Google ScholarGoogle ScholarDigital LibraryDigital Library
  24. Robert Harper and Mark Lillibridge. A type-theoretic approach to higher-order modules with sharing. In Principles of Programming Languages, pages 123--137, 1994. Google ScholarGoogle ScholarDigital LibraryDigital Library
  25. Gregor Kiczales and Mira Mezini. Aspect-oriented programming and modular reasoning. In ACM: International Conference on Software engineering, pages 49--58, 2005. Google ScholarGoogle ScholarDigital LibraryDigital Library
  26. Gregor Kiczales, John Lamping, Anurag Menhdhekar, Chris Maeda, Cristina Lopes, Jean-Marc Loingtier, and John Irwin. Aspect-oriented programming. In Mehmet Aks 'it and Satoshi Matsuoka, editors, LNCS 1241: European Conference on Object-Oriented Programming, pages 220--242, 1997.Google ScholarGoogle Scholar
  27. Xavier Leroy. Manifest types, modules, and separate compilation. In Principles of Programming Languages, pages 109--122, 1994. Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. Doug McIlroy. Mass produced software components. In Naur and Randell (1969), pages 138--155.Google ScholarGoogle Scholar
  29. Lambert Meertens. Paramorphisms. Formal Aspects of Computing, 4(5):413--425, 1992.Google ScholarGoogle ScholarCross RefCross Ref
  30. Erik Meijer and Graham Hutton. Bananas in space: Extending fold and unfold to exponential types. In Functional Programming Languages and Computer Architecture, pages 324--333, 1995. Google ScholarGoogle ScholarDigital LibraryDigital Library
  31. Bertrand Meyer. Object-Oriented Software Construction. Upper Saddle River, N.J., Prentice Hall, 2nd edition, 1997. Google ScholarGoogle ScholarDigital LibraryDigital Library
  32. Bertrand Meyer and Karine Arnout. Componentization: The Visitor example. Computer, 39(7):23--30, 2006. Google ScholarGoogle ScholarDigital LibraryDigital Library
  33. Donald Michie. Memo functions and machine learning. Nature, 218:19--22, 1968.Google ScholarGoogle ScholarCross RefCross Ref
  34. Adriaan Moors, Frank Piessens, and Martin Odersky. Towards equal rights for higher-kinded types. In 6th International Workshop on Multiparadigm Programming with Object-Oriented Languages, 2007.Google ScholarGoogle Scholar
  35. Peter Naur and Brian Randell, editors. Software Engineering: Report of a Conference Sponsored by the NATO Science Committee, Garmisch, Germany, 7-11 Oct. 1968. 1969.Google ScholarGoogle Scholar
  36. Peter Norvig. Design patterns in dynamic programming. In Object World 96, May 1996.Google ScholarGoogle Scholar
  37. Martin Odersky. An Overview of the Scala programming language (second edition). Technical Report IC/2006/001, EPFL Lausanne, Switzerland, 2006.Google ScholarGoogle Scholar
  38. Martin Odersky and Matthias Zenger. Independently extensible solutions to the expression problem. In Proceedings of Foundations of Object-Oriented Languages 12, January 2005a. http://homepages.inf.ed.ac.uk/wadler/fool.Google ScholarGoogle Scholar
  39. Martin Odersky and Matthias Zenger. Scalable component abstractions. In Object Oriented Programming, Systems, Languages, and Applications, pages 41--57, 2005b. Google ScholarGoogle Scholar
  40. Bruno C.d.S. Oliveira. Genericity, Extensibility and Type-Safety in the Visitor Pattern. PhD thesis, University of Oxford, 2007.Google ScholarGoogle Scholar
  41. Doug Orleans and Karl Lieberherr. DJ: Dynamic adaptive programming in Java. In Reflection 2001: Meta-level Architectures and Separation of Crosscutting Concerns, Kyoto, Japan, September 2001. Springer Verlag. Google ScholarGoogle ScholarDigital LibraryDigital Library
  42. Jens Palsberg and C. Barry Jay. The essence of the Visitor pattern. In Computer Software and Applications, pages 9--15, 1998. Google ScholarGoogle ScholarDigital LibraryDigital Library
  43. Michel Parigot. Recursive programming with proofs. Theoretical Computer Science, 94(2):335--356, 1992. Google ScholarGoogle ScholarDigital LibraryDigital Library
  44. Simon Peyton Jones, editor. Haskell 98 Language and Libraries - The Revised Report. Cambridge University Press, Cambridge, England, 2003.Google ScholarGoogle Scholar
  45. Cuno Pfister and Clemens Szyperski. Why objects are not enough. In International Component Users Conference, 1996. Google ScholarGoogle ScholarDigital LibraryDigital Library
  46. ZdzisBaw SpBawski and PaweB Urzyczyn. Type fixpoints: Iteration vs. recursion. In International Conference on Functional Programming, pages 102--113, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  47. Clemens Szyperski. Independently extensible systems - software engineering potential and challenges. In 19th Australian Computer Science Conference, 1996.Google ScholarGoogle Scholar
  48. Peri Tarr, Harold Ossher, William Harrison, and Stanley Sutton, Jr. N degrees of separation: Multi-dimensional separation of concerns. In International Conference on Software Engineering, pages 107--119, 1999. Google ScholarGoogle ScholarDigital LibraryDigital Library
  49. Thomas VanDrunen and Jens Palsberg. Visitor-oriented programming. In Proceedings of FOOL-11, the 11th ACM SIGPLAN International Workshop on Foundations of Object-Oriented Languages, New York, NY, USA, January 2004. ACM Press.Google ScholarGoogle Scholar
  50. Joost Visser. Visitor combination and traversal control. In Object Oriented Programming, Systems, Languages, and Applications, pages 270--282. ACM, 2001. Google ScholarGoogle ScholarDigital LibraryDigital Library

Index Terms

  1. The visitor pattern as a reusable, generic, type-safe component

          Recommendations

          Comments

          Login options

          Check if you have access through your login credentials or your institution to get full access on this article.

          Sign in

          PDF Format

          View or Download as a PDF file.

          PDF

          eReader

          View online with eReader.

          eReader
          About Cookies On This Site

          We use cookies to ensure that we give you the best experience on our website.

          Learn more

          Got it!