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.

Allowing an object to change its behavior

Allowing an object to change its behavior - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 2

Start my 1-month free trial

Allowing an object to change its behavior

- [Instructor] The first thing I'm going to do now is I'm going to create an interface for the state of the MediaPlayer. So I'm going to go ahead and create a new Java interface. And I'm going to call it State. I want to move the functionality in the MediaPlayer that depends on its state into the implementations of this interface. So I'm going to define play and pause methods. So first, I'm going to do void pause and this is going to take a MediaPlayer object as an argument. So I'm going to pass in a mediaPlayer. Then I'll do void play. And again, I'm going to pass in a MediaPlayer object. So now I'm going to make the concrete implementations of this interface. So I'm going to make another new Java class. And I'm going to call this one PlayingState. And this is going to implement my state interface. I need to make sure I've got the methods it defines, so I'm going to add public void play. And public void pause. Now…

Contents