Visitor design pattern in Java

Visitor design pattern in Java


visitor pattern is a type of behavioral
design pattern this is mainly used when we need to perform an operation on a
group of similar kind of objects with the help of visitor pattern we can move
the operational logic from the object to a group of object as for example instead
of providing offer to particular fruit or vegetables we can Club them together
and offer 10% for electronics and 2% discount on groceries and then combined
together and populate the total balance let’s jump into code and check how to
implement it in Java we will create shoppingCartVisitor interface and have two type of visit input or we
can say two groups of element computer another visit method with input printer next we will have item interface and
have an abstract method accept which takes an object of type shoppingCartVisitor it’s confusing but everything will get clarified at the end stay tuned
now let’s have our computer which is of type item we will have member variables like cost
serial number and other setter and getter methods and in the constructor of computer we
will overload it and assign to this.cost and this.serialNumber and have our getter
function to fetch the price another getter method to get serial
number details and it overridden accept method we will
return visitor.visit with current object and the return type will be
integer similarly we will have printer class
which implements item interface and in constructor we will accept price
of cartridge number of cartridges and model number of the cartridge and assign it to the member variables and have each getter function get price
of cartridge and return price of cartridge get number of cartridge and return the
number of cartridge get model number and return this.model number and in accept method we will return integer that is visitor.visit now we will have the shopping cart
visitor implementation which will group each item types and in visit printer method we will
print out the printer details and return the printer cost but in visit computer method we will apply $50 discount if the computer price is greater than 15,000 else we will calculate the actual cost now let’s have our client class or we
can say customer class we will have an array of items computer of cost
$20,000 and serial number 1234 and the computer of cost $14,500 and printer with price $1,000 and 6
cartridges of type HP and let’s say another Xerox printer of
price $975 let’s calculate the total price so we will loop the item array and each item.accept will provide
the required cost after any discount so using this visitor pattern it’s easy to
add any new items or change existing item behavior without changing the
actual class object just by twisting the visitor implementation class that’s all for this tutorial don’t
forget to hit the like button share and subscribe for upcoming video tutorials
see you again in next tutorial

1 thought on “Visitor design pattern in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *