Java Magazine, Sept/Oct 2018
The Visitor Design Pattern in Depth Perform one or more operations on a collection of different data types without disrupting existing code ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 66 design patterns Suppose its your first day at a new job at a midsize company Youll probably be escorted around the building and introduced to every department of the organization At each one youll say Glad to meet you a few times and talk with the team there to discuss your common projects and then youll say Nice to have met you And youll repeat this for each department Congratulations You have just implemented the Visitor design pattern in humanware The Pattern Visitor is a useful pattern when you have many objects of diferent types in your data structure and you want to apply some operation to several or all of them The pattern is helpful when you dont know ahead of time all the operations you will need it gives you flexibility to add new operations without having to add them to each object type The basic idea is that a Visitor object is taken around the nodes of a data structure by some kind of iterator and each node accepts the visitor allowing it access to that node objects internal data When a new function is needed only a new visitor needs to be written The iteration is conceptually simple for Node node collection node accept visitor There are two main code examples in this article both can be found in my GitHub repository IAN DARWIN
You must have JavaScript enabled to view digital editions.