From the course: Complete Guide to Java Design Patterns: Creational, Behavioral, and Structural

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Challenge: The Visitor pattern

Challenge: The Visitor pattern

In this final challenge, you'll be implementing the visitor pattern in a ticketing system app. So firstly, there's an interface called TicketElement, which has a single method called getPrice. Then there are two implementations of this. So the first one is the AdultTicket class. And in here the price is initially set to 10. Then in the getPrice method, we return that price field, and there's also a setPrice method to change the price. The other class that implements TicketElement is the GroupTicket class. So in this one there's a single field called elements which is an array list of other ticket elements. Then in the getPrice method, it actually gets all of the prices of the tickets in the list and adds them together. Then there's a method called addElement to add a ticket elements to the list and getElements which returns the list. Finally, there's the App class, which has a main method. So in here we're creating a group ticket and then two adult tickets which we're then adding to…

Contents