From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Challenge: The Template Method pattern

Challenge: The Template Method pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Challenge: The Template Method pattern

(upbeat electronic music) - [Instructor] Let's have a look at the next exercise. In this one, there's a simple game that prints out the actions taken by different characters in the game. And in this exercise, you should refactor the existing codes to avoid duplication by making use of the template method pattern. So in this game, there are two classes, called pirate and troll, which represent different characters in the game. And in this main class I've got here, there's a main method where one of each type of character is created, and then a method called defendAgainstAttack is called on each of them. So if I run the app, I can see that it prints out in the console the action taken by each character. So the pirate picks up a sword, defends with the sword, and then returns to the ship. And the troll picks up the club, defends with the club, and then returns to the mountain. After you've refactored the app, the output…

Contents