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.

Solution: The Template Method pattern

Solution: The Template Method pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Solution: The Template Method pattern

(bright music) - [Instructor] Now let's see my solution to the exercise. So I've created a new abstract class called Character. This contains several methods that are defined but empty, which are pickUpWeapon, defenseAction, and moveToSafety. These are all different in both the pirate and troll classes, so I've just defined them here. If you haven't put these methods in the abstract class, that's totally fine, it will still work the same. I've chosen to include them because they are a bit like defining methods in an interface and it tells me that if I want to create further concrete implementations of this class, I should override these methods. I've also added defendAgainstAttack, which was the same in both classes. I've also removed that method from both the pirate and the troll classes. So for example, in this pirate class, it now extends the character class, and the defend against attack method is no longer there. So…

Contents