Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 75 design patterns A slightly fancier version might indent one tab stop for each directory level Theres a start at making such a thing in the class IndentingFileVisitor in my GitHub repository although it doesnt work superbly yet To try it just change the instantiation of the FileVisitor in the main method Conclusion Besides the examples of the word processor add on and directory navigation are there other uses of the Visitor pattern Certainly Examples include its use in compilers as mentioned earlier report writing where diferent people need diferent reports and graphics programs in short any application in which you need to add functionality across a hierarchy without disrupting or even changing the nodes in the hierarchy Visitor is like walking around a new company visiting all the teams and having them accept you the introductions and give you their impressions of your job the visit The Visitor pattern allows you to retain flexibility to add new methods at a slight cost the reduction of encapsulation and the need for every visitor to know about all the diferent node types Its not a onesize fits all pattern Its optimal when the number of functionalities that you might have to add is significantly greater than the number of node types in your data structure If the number of data types node types to be added is greater than the functions youll need to add or you truly know that you wont need to add new functions very often dont use this pattern but for the other cases youll find Visitor to be an elegant solution article Ian Darwin @ Ian_ Darwin has done all kinds of development from mainframe applications and desktop publishing applications for UNIX and Windows to a desktop database application in Java to healthcare apps in Java for Android Hes the author of Java Cookbook and Android Cookbook both from OReilly He has also written a few courses and taught many at Learning Tree International Previous design patterns presented in this series State Pattern Command Pattern
You must have JavaScript enabled to view digital editions.