1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Design Patterns in Java Projects

Visitor

Contents

keyboard_tab
Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
4h 56m
Students
19
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello dear friends. In this video, we will examine the visitor design pattern. Let's begin. In object-oriented programming and software engineering, the visitor design pattern is 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's one way to follow the open-closed principal. The purpose of a visitor pattern is to define a new operation without introducing modifications to an existing object structure. This pattern comes under behavior pattern category. In visitor pattern, we use a visitor class which changes the executing algorithm of an element class. By this way, execution algorithm of elements can vary as and when visitor varies.

This pattern comes under behavior pattern category. As per the pattern, element object has to accept the visitor object, so that visitor object handles the operation on the element object. Let's examine the participants of visitor pattern on UML. Visitor is an interface or abstract class used to declare the visit operations for all the types of visitable classes. For each type of visitor, all the visit methods declared in abstract visitor must be implemented. Each visitor will be responsible for different operations. This will be our concrete visitor. Visitor is an interface which declares the accept operation. This is the entry point which enables an object to be visited by the visitor object. Concrete visitable classes implements the visitable interface or class and defines the accept operation.

The visitor object is parsed to this object using the accept operation. Visitor pattern simplifies the addition of new operations and also it gathers related operations while separating unrelated ones. We should know the return type of visit methods at the time of designing. Otherwise, we will have to change the interface and all of its implementations. If there are too many implementations of visitor interface, it makes it hard to extend. When an object structure contains many objects with different interfaces and there is a need to perform operations on these objects depending on their concrete class or many different and unrelated operations must be performed on objects in a structure or when classes defined the object structure, you can use the visitor pattern. Now, if you'd like, let's create a project and apply the visitor pattern. Create a new project and name it visitor. The name is visitor.

So, let's make a little world tour. First, we need an interface. Let's name it country visitor interface. Let's define countries in this interface; Italy, USA, and Canada. Now we need an interface named country. Let's add a method to call country visitor. This will take country visitor interface as parameter. Let's define countries one-by-one. First start with Italy, implements country. Now, override method. Go on with USA. Last one is Canada. Let's define countries list as a class, name will be countries. 

First, we need an array. In constructor, let's add our countries, implements country. Now we will override method in country, done. Let's define concrete elements. We have to override countries method. Italy, USA, and Canada, done. Now the structure is ready. Let's use. First, define an instance from countries, define visitor interface on visitor. Now call countries method. Now, when we run project, we can see the countries that we have visited. Let's try. Look, the result, Italy, USA, and Canada, done. We have now finished looking at the visitor pattern. In the next video, we will examine the mediator design pattern. See you in the next video.

 

About the Author
Students
413
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics