Design Pattern

How to implement Visitor Design Pattern using Java?

Rohan Ravindra Kadam
Javarevisited
Published in
5 min readJan 15, 2022

--

Hello👋 Its Rohan Kadam😊

Maybe you are a newbie to coding👩‍💻 or programming, maybe experienced👴 or maybe a FrontEnd Developer or BackEnd Developer you all have may have come across the word Design Pattern as a principle or as a concept. The design pattern is a way or approach to writing code or developing an application. In this article, we try to answer certain questions What is Design Pattern? Why implement a design pattern? How to implement a Design pattern (Visitor)?

How to implement Visitor Design Pattern using Java?

🏭What is Design Pattern?

Design patterns are the solutions to commonly occurring problems in software design. They are like pre-fabricated blueprints that we can customize to solve a recurring design problem in your code.

They are not pieces of code or libraries that can be imported and used in the development of software or solving a particular problem. We can follow the pattern details and implement a solution that suits the realities of your own program.

We often confused ourselves between design patterns and algorithms. While an algorithm always defines a clear set of actions that can achieve some goal, a pattern is a more upper-level description of a solution.

An analogy to an algorithm is a cooking dish: both have clear steps to achieve a goal. They’re not libraries or modules; they’re guidelines you integrate into the core of your designs, giving you a leg up in creating flexible and maintainable object-oriented systems.

⚡Why implement a Design Pattern?

We have encountered design patterns only in a nutshell, they’re general object-oriented solutions that you can use in your own designs. Crafted by experienced 👴object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. So, if design patterns still aren’t part of your development toolbelt, here are five reasons you should add them.

1 → Make our life easier by not reinventing the wheel

2 → Improve our object-oriented Skills

3 → Recognize Patterns in libraries and languages

4 → Use the power of a shared vocabulary

5 → Find Truth and beauty

⚡How to implement a Visitor Design Pattern?

Before jumping directly into the implementation of the design pattern we need to answer certain questions such as What is a Visitor design pattern? Why incorporate the design pattern? and finally How to implement a Visitor Design Pattern?

1 →What is Visitor Design Pattern?

The Visitor design pattern is part of the Behavioral Design Pattern Family, it let us create separate an algorithm from an Object Structure on which it operates. A real-life example will be, Imagine we running a delivery business as result we are responsible for delivering different types of packages. Due to differences in the types of packages, the pricing will also be different — we used visitor patterns to aggregate the total cost of delivery for the package. That’s how the Visitor pattern works in real life.

Definition:
The Visitor pattern is a design pattern that allows us to separate an algorithm from an Object Structure on which it operates.e

2 → Why implement Visitor Design Pattern?

  1. Visitor allows us to follow the Design Principle (SOLID).
  2. It fosters loose-coupling by eliminating the need to bind application-specific classes into the code.
  3. It allows us to introduce new code without affecting the existing code structure like Plug and Play Model

3 → How to Implement Visitor Design Pattern?

In section, we try to implement the Visitor design Pattern using Java. For example, we used delivery management for finding out the total cost for packages to deliver based on size.

⚡How will we visit the element?

To allow the visitor to visit the elements we have created a Visitable Interface that accepts visitors.

Visitable Interface

⚡What will be the different elements that will be visited?

In our case, we have three elements(packages) which are below. We have implemented Visitable to make elements(packages) visitable by the visitor.

Small Package Element

Medium Package Visitor Element

Medium Package (Visitor element)

Large Package (Visitor Package)

Large Package(Visitor Element)

⚡How will visitors know which elements to visit?

We have created a visitor interface that accepts different visitors(packages) it may be small, medium or large.

Visitor Class

⚡How will calculate end results based on visitors elements?

We have created a Package visitor that helps us to calculate the total cost of delivery for all different packages types(visitor elements)

Package Visitor with total Cost

⚡How to add Visitor Elements to calculate the total cost of delivery?

Design Pattern Class

⚡Where to find the Codebase for Visitor Design Pattern?

⚡Conclusion:-

In the article, we tried to answer some questions related to Design Pattern and especially Visitor Design Pattern how it helps developers around the world to write better code and build faster applications. Visitor Design Pattern allows us to implement the SOLID principle effectively. The best examples of Visitor Design Patterns will be Shopping carts, delivery businesses etc. Please do share and like💖 if you find the article useful. Follow me on medium Rohan Ravindra Kadam and on Twitter at rohankadam25

Do not forget to check out my website to know more about me

📚Bibliography:-

Thank You — Rohan Kadam

--

--