Design Pattern

Visitor Pattern Design Patterns in Java

<p>This article explains about the <strong>visitor pattern<&sol;strong> of the<strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;design-patterns&lowbar;25&sol;"> GoF Design Behavior patterns<&sol;a><&sol;strong>&period; In the <strong>visitor design pattern<&sol;strong>&comma; the visitor class is used to alter the algorithm that is executing as part of the element class&period; This is an easy methodology to control the algorithm executing for elements by using the visitor class&comma; as the executing algorithm varies with the variance in the visitor class&period;<&sol;p>&NewLine;<p>The Visitor class is considered as a type of <strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;behavioral-design-pattern&sol;">Behavior pattern<&sol;a><&sol;strong> that presents an implementation of the exchange of place with the exchange of control mechanism&period; As the object of the element&comma; the class must provide a place to the object of the visitor class in order for it to control the element class&period; The visitor class is the controller of all variance in the operations of the element class&period;<&sol;p>&NewLine;<h2>Visitor Pattern<&sol;h2>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>According to the Gang of Four&colon;<&sol;strong><&sol;p>&NewLine;<div style&equals;"text-align&colon; left&semi;">Represent an operation to be performed on the elements of an object structure&period; Visitor lets you define a new operation without changing the classes of the elements on which it operates&period;<&sol;div>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<div style&equals;"text-align&colon; left&semi;">&NewLine;<blockquote><p><strong>also read&colon;<&sol;strong><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;observer-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;observer-pattern-design-patterns-java&sol;">Observer Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;chain-responsibility-pattern&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;chain-responsibility-pattern&sol;">Chain of responsibility Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;command-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;command-pattern-design-patterns-java&sol;">Command Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;strategy-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;strategy-pattern-design-patterns-java&sol;">Strategy Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;template-method-pattern-design-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;template-method-pattern-design-java&sol;">Template method Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interpreter-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;interpreter-pattern-design-patterns-java&sol;">Interpreter Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;iterator-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;iterator-pattern-design-patterns-java&sol;">Iterator Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;mediator-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;mediator-pattern-design-patterns-java&sol;">Mediator Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;memento-pattern-design-patterns-java&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;memento-pattern-design-patterns-java&sol;">Memento Design Pattern<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;creational-design-patterns&sol;&sol;">Creational GoF Design Patterns<&sol;a><&sol;li>&NewLine;<li><a title&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;structural-design-patterns&sol;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;structural-design-patterns&sol;">Structural GoF Design Patterns<&sol;a><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;blockquote>&NewLine;<&sol;div>&NewLine;<h2>UML Class Diagram<br &sol;>&NewLine;<img class&equals;"aligncenter wp-image-3466 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;12&sol;visitor&period;gif" alt&equals;"visitor pattern" width&equals;"467" height&equals;"533" &sol;><&sol;h2>&NewLine;<h3>Visitor Class<&sol;h3>&NewLine;<ul>&NewLine;<li>The visitor class performs the mechanism of controlling any sort of variance in the element class&period; The object of the visitor class resides in the element class for control&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3>Element Class<&sol;h3>&NewLine;<ul>&NewLine;<li>The element class is the main class performing critical operations whose variations are controlled by the visitor class&period;<&sol;li>&NewLine;<li>The operation that is performed on the element is represented through visitor behavioural pattern&period; A new operation can be defined by the Visitor class without any alteration to the normal classes belonging to the element being operated on&period; Many operations that are not related to each other are performed on a structure that is composed of heterogeneous node objects which can pollute the classes of the node objects&period;<&sol;li>&NewLine;<li>To avoid pollution&comma; querying is performed on nodes to determine the correct type and point the operation to it&period; This is quite inefficient as it cost a lot of time and resources for querying every node and waiting for a response&period; The Visitor Behavioral Pattern provides a solution with this by abstracting functionalities on element objects aggregated together in a hierarchical structure&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Pattern<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use an author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&amp&semi;ie&equals;UTF8&amp&semi;qid&equals;1512607966&amp&semi;sr&equals;1-1&amp&semi;keywords&equals;dinesh&plus;rajput" target&equals;"&lowbar;blank" rel&equals;"noopener noreferrer"><img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern-243x300&period;jpg" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<p>The Element classes are created in such a way that includes only the necessary functionalities and the Visitor subclasses are created that provide the additional functionality for the Element class&period; A double dispatch mechanism is implemented by the Visitor Class in which&comma; the request’s name and the Visitor type and Element type both act as receivers these things determine the operation that will be performed&period;<&sol;p>&NewLine;<h2>Visitor Design Pattern Implementation<&sol;h2>&NewLine;<p>This can be implemented by creating a Visitor hierarchy through the pure virtual class of the visitor&period; A method visit&lpar;&rpar; is declared in the base class of the Visitor and it is overloaded in the child&sol;derived classes by using the specific methodology of querying and casting&period; The visitor&lpar;&rpar; method receives the pointer to the derived class for the Element class&period; The base class of the Element class has an accept&lpar;&rpar; method which accepts the pointer to the base class of the Visitor class&period; The accept&lpar;&rpar; method can be implemented on the Element’s concrete derived class by calling the visit&lpar;&rpar; method on the Visitor’s derived instance&period;<&sol;p>&NewLine;<p>The accept&lpar;&rpar; method is responsible for the selection of the Element class to be controlled and the visit&lpar;&rpar; method allocated the control to the correct Visitor instance&period; This allows related and unrelated operations to be performed on an object that is aggregated into a single hierarchical structure&period;<&sol;p>&NewLine;<h2>Visitor Pattern Sample Example<&sol;h2>&NewLine;<p>In this sample real-world example&comma; we demonstrate the Visitor pattern in which two objects traverse a list of Employees and perform the same operation on each Employee&period; There are two visitor objects define different operations&period; These operations are one adjusts vacation days and the other income&period;<&sol;p>&NewLine;<p>VisitorPatternDemo&comma; our demo class&comma; will use Employee and Visitor classes to demonstrate the use of visitor pattern&period;<&sol;p>&NewLine;<h3>Step 1&colon; Define an interface to represent element&period;<br &sol;>&NewLine;Element&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public interface Element &lbrace;&NewLine;&Tab;&NewLine;&Tab;void accept&lpar;IVisitor visitor&rpar;&semi;&NewLine;&rcub;&NewLine;<&sol;pre>&NewLine;<h3>Step 2&colon; Create concrete classes extending the above class&period;<br &sol;>&NewLine;Employee&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class Employee implements Element &lbrace;&NewLine;&Tab;&NewLine;&Tab;private String &lowbar;name&semi;&NewLine; private double &lowbar;income&semi;&NewLine; private int &lowbar;vacationDays&semi;&NewLine; &NewLine; &NewLine;&Tab;public Employee&lpar;String &lowbar;name&comma; double &lowbar;income&comma; int &lowbar;vacationDays&rpar; &lbrace;&NewLine;&Tab;&Tab;super&lpar;&rpar;&semi;&NewLine;&Tab;&Tab;this&period;&lowbar;name &equals; &lowbar;name&semi;&NewLine;&Tab;&Tab;this&period;&lowbar;income &equals; &lowbar;income&semi;&NewLine;&Tab;&Tab;this&period;&lowbar;vacationDays &equals; &lowbar;vacationDays&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;&commat;Override&NewLine;&Tab;public void accept&lpar;IVisitor visitor&rpar; &lbrace;&NewLine;&Tab;&Tab;visitor&period;visit&lpar;this&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;public String get&lowbar;name&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;return &lowbar;name&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;public void set&lowbar;name&lpar;String &lowbar;name&rpar; &lbrace;&NewLine;&Tab;&Tab;this&period;&lowbar;name &equals; &lowbar;name&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;public double get&lowbar;income&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;return &lowbar;income&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;public void set&lowbar;income&lpar;double &lowbar;income&rpar; &lbrace;&NewLine;&Tab;&Tab;this&period;&lowbar;income &equals; &lowbar;income&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;public int get&lowbar;vacationDays&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;return &lowbar;vacationDays&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&Tab;public void set&lowbar;vacationDays&lpar;int &lowbar;vacationDays&rpar; &lbrace;&NewLine;&Tab;&Tab;this&period;&lowbar;vacationDays &equals; &lowbar;vacationDays&semi;&NewLine;&Tab;&rcub;&NewLine;&Tab;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Director&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class Director extends Employee &lbrace;&NewLine;&NewLine;&Tab;public Director&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;super&lpar;"Anamika"&comma; 45000&period;0&comma; 20&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>President&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class President extends Employee &lbrace;&NewLine;&NewLine;&Tab;public President&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;super&lpar;"Arnav"&comma; 65000&period;0&comma; 25&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Clerk&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class Clerk extends Employee &lbrace;&NewLine;&NewLine;&Tab;public Clerk&lpar;&rpar; &lbrace;&NewLine;&Tab;&Tab;super&lpar;"Dinesh"&comma; 35000&period;0&comma; 18&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Employees&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;import java&period;util&period;ArrayList&semi;&NewLine;import java&period;util&period;List&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class Employees &lbrace;&NewLine;&Tab;&NewLine;&Tab;&sol;&sol; Setup employee collection&NewLine;&Tab;List empList &equals; new ArrayList&lt&semi;&gt&semi;&lpar;&rpar;&semi;&NewLine;&Tab;&NewLine;&Tab;public void add&lpar;Employee employee&rpar;&lbrace;&NewLine;&Tab;&Tab;empList&period;add&lpar;employee&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void remove&lpar;Employee employee&rpar;&lbrace;&NewLine; &Tab;empList&period;remove&lpar;employee&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; public void accept&lpar;IVisitor visitor&rpar;&lbrace;&NewLine; &Tab;for&lpar;Employee employee &colon; empList&rpar;&Tab;&lbrace;&NewLine; &Tab;&Tab;employee&period;accept&lpar;visitor&rpar;&semi;&NewLine; &Tab;&rcub;&NewLine; &Tab;System&period;out&period;println&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Step 3&colon; Define an interface to represent visitor&period;<br &sol;>&NewLine;IVisitor&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public interface IVisitor &lbrace;&NewLine;&Tab;&NewLine;&Tab;void visit&lpar;Element element&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Step 4&colon; Create a concrete visitor implementing the above class&period;<br &sol;>&NewLine;IncomeVisitor&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class IncomeVisitor implements IVisitor &lbrace;&NewLine;&NewLine;&Tab;&commat;Override&NewLine;&Tab;public void visit&lpar;Element element&rpar; &lbrace;&NewLine;&Tab;&Tab;Employee employee &equals; &lpar;Employee&rpar; element&semi;&NewLine;&Tab;&Tab;&sol;&sol; Provide 10&percnt; pay raise&NewLine;&Tab;&Tab;employee&period;set&lowbar;income&lpar;employee&period;get&lowbar;income&lpar;&rpar;&ast;1&period;10&rpar;&semi;&NewLine;&Tab;&Tab;System&period;out&period;println&lpar;employee&period;getClass&lpar;&rpar;&period;getSimpleName&lpar;&rpar;&plus;" "&plus;employee&period;get&lowbar;name&lpar;&rpar; &plus;"'s new income&colon; "&plus;employee&period;get&lowbar;income&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>VacationVisitor&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class VacationVisitor implements IVisitor &lbrace;&NewLine;&NewLine;&Tab;&commat;Override&NewLine;&Tab;public void visit&lpar;Element element&rpar; &lbrace;&NewLine;&Tab;&Tab;Employee employee &equals; &lpar;Employee&rpar; element&semi;&NewLine;&Tab;&Tab;&sol;&sol; Provide 3 extra vacation days&NewLine;&Tab;&Tab;employee&period;set&lowbar;vacationDays&lpar;employee&period;get&lowbar;vacationDays&lpar;&rpar;&plus;3&rpar;&semi;&NewLine;&Tab;&Tab;System&period;out&period;println&lpar;employee&period;getClass&lpar;&rpar;&period;getSimpleName&lpar;&rpar;&plus;" "&plus;employee&period;get&lowbar;name&lpar;&rpar; &plus;"'s vacation days&colon; "&plus;employee&period;get&lowbar;vacationDays&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Step 5&colon; VisitorPatternDemo&comma; our demo class&comma; will use Employee and Visitor classes to demonstrate the use of visitor pattern&period;<&sol;h3>&NewLine;<h3>&NewLine;VisitorPatternDemo&period;java<&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast;&NewLine; &ast; &NewLine; &ast;&sol;&NewLine;package com&period;doj&period;patterns&period;behavior&period;visitor&semi;&NewLine;&NewLine;&sol;&ast;&ast;&NewLine; &ast; &commat;author Dinesh&period;Rajput&NewLine; &ast;&NewLine; &ast;&sol;&NewLine;public class VisitorPatternDemo &lbrace;&NewLine;&Tab;&NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab;&sol;&sol; Setup employee collection&NewLine;&Tab;&Tab;Employees employees &equals; new Employees&lpar;&rpar;&semi;&NewLine;&Tab;&Tab;employees&period;add&lpar;new Clerk&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&Tab;employees&period;add&lpar;new Director&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&Tab;employees&period;add&lpar;new President&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&Tab;&NewLine;&Tab;&Tab; &sol;&sol; Employees are 'visited'&NewLine;&Tab;&Tab;employees&period;accept&lpar;new IncomeVisitor&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&Tab;employees&period;accept&lpar;new VacationVisitor&lpar;&rpar;&rpar;&semi;&NewLine;&Tab;&rcub;&NewLine;&rcub;&NewLine;&NewLine;<&sol;pre>&NewLine;<h3>Step 6&colon; Let&&num;8217&semi;s run this demo class and verify the output&period;<&sol;h3>&NewLine;<pre class&equals;"highlight">Clerk Dinesh's new income&colon; 38500&period;0&NewLine;Director Anamika's new income&colon; 49500&period;00000000001&NewLine;President Arnav's new income&colon; 71500&period;0&NewLine;&NewLine;Clerk Dinesh's vacation days&colon; 21&NewLine;Director Anamika's vacation days&colon; 23&NewLine;President Arnav's vacation days&colon; 28&NewLine;&NewLine;<&sol;pre>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;chain-responsibility-pattern&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;memento-pattern-design-patterns-java&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '3465'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago