From the course: Java Design Patterns: Behavioral Part 1

Unlock the full course today

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

Solution: The Command pattern

Solution: The Command pattern - Java Tutorial

From the course: Java Design Patterns: Behavioral Part 1

Start my 1-month free trial

Solution: The Command pattern

(upbeat music) - [Instructor] Now let's have a look at my solutions to the exercise. Don't worry if yours looks a bit different to mine, that's totally fine. So I've created an interface called commands, which has a single method called execute. Then I have two concrete implementations of this interface called place order command and return order command. These both have a field called jacket and the jacket gets passed into the constructor. Then in the execute method, I'm coding the appropriate method on the jacket. So in this return order commands class in the execute method, I'm doing jacket.return order. Finally, in the sales order client's class. Where I'm coding the invoke method on the handlers. I'm now passing in the expected commands. So if I run the app now I can see it prints out jacket order has been placed and jacket order has been returned.

Contents