Visitor Design Pattern In Java

Visitor Design Pattern In Java

The Visitor design pattern lets us separate algorithms from the objects on which they operate. See how to implement it to follow the open/closed principle.

Today, I am here to discuss another behavioral design pattern called Visitor Design Pattern. The Visitor design pattern lets us separate algorithms from the objects on which they operate.

Visitor Design Pattern

  • The Visitor Design Pattern provides a way of separating an algorithm from an object structure on which it operates.
  • A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures. It is one way to follow the open/closed principle
  • The Visitor Design Pattern is used when we like to perform an operation on a group/family of objects.
  • The Visitor Design Pattern is one of the twenty-three well-known GoF design patterns which helps us to move the operational logic from the objects to another class.
  • The *Visitor *allows adding new virtual functions to a family of classes, without modifying the classes.
  • Instead, a *visitor *class is created that implements all of the appropriate specializations of the virtual function. 
  • The Visitor Design Pattern makes it possible to define a new operation for (some) classes of an object structure without changing the classes.
  • When new operations are needed frequently and the object structure consists of many unrelated classes, it's inflexible to add new sub-classes each time a new operation is required.
  • The Element _accepts _the visitor and delegates the operation to the accepted *Visitor *object.
  • The *Visitor *object performs the operation on the element ("visits the element").
  • The Visitor Design Pattern makes it possible to create _new operations independently_ from the classes of an object structure by adding new visitor objects.

visitor design pattern UML diagram

To understand this, let's take an example of a Shop, which sells books, fruits, vegetables, and electronics.

java tutorial design pattern visitor design pattern

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Proxy Design Pattern in JAVA

In this blog, we will discuss Proxy Design Pattern, its example, and how it is different from the other design patterns. But, it’s important to have an understanding of the design patterns first. You can refer here.

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Java Design Patterns for Beginners - Design Patterns in Java

"Java Design Patterns for Beginners - Design Patterns in Java - Design Patterns Tutorial" will provide you with detailed knowledge about Java Design Patterns and along with it. Why do we need Design Patterns? Structure of Design Patterns. Types of Design Patterns. Creational Design Patterns. Factory Design Pattern. Overview of Design Patterns

Composite Design Pattern in Java

This post looks at how to use the composite design pattern in Java, specifically the objects used in the pattern: component, composite, leaf, and client.

Memento Design Pattern In Java

Today, I would like to discuss another behavioral design pattern called the Memento Design Pattern which is used to restore the state of an object to a previous state.