Shopping Cart:

In this example, the visitor design pattern is used to visit two different objects of Book and Fruit classes and to extract data from them by passing Visitor object to their accept methods.

In the main method of ShoppingCartClient class, Book and Fruit objects are created as items and added in an array of ItemElementObject. In the calculatePrice method of the main class (ShoppingCartClient), ShoppingCartVisitorImpl object is created and is passed as a parameter for each of the item's accept method call. In the accept method of the Element (Fruit,Book) class, the visit method of the ShoppingCartVisitorImpl is called with passing the Element's object as a parameter. In the visit method, necessary data is calculated about related Element object.

the generated java code of the shopping case study is below.

22-nisan-shopping-java.pdf