From the course: Java Design Patterns: Behavioral Part 2

Unlock the full course today

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

Challenge: The State pattern

Challenge: The State pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Challenge: The State pattern

(upbeat music) - [Instructor] Let's have a look at the next exercise. In this one, you're going to be refactoring an app that controls the temperature using a fan. So let's have a quick look at the code. First, there is this class called fan. This is a single field called state, which is a string and it's set to low. In the main class, I have a main method where I'm creating a fan object and then the turn up and turn down methods are called a few times. So if I run the app, I can see it prints out a message to the console every time the fan is turned up or down, and it can either be low, medium or high. In this exercise, you should refactor this app to use the state pattern, instead of having these long switch statements in the fan class. The output when you run the app, shouldn't change, I'm estimating this will take you about 15 to 20 minutes, so good luck and have fun.

Contents