From the course: Complete Guide to Java Design Patterns: Creational, Behavioral, and Structural
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Solution: The Command pattern - Java Tutorial
From the course: Complete Guide to Java Design Patterns: Creational, Behavioral, and Structural
Solution: The Command pattern
So this is my solution to the command pattern exercise. The first thing I'm going to do is create an interface for executing commands. So I'm going to create a new Java class and I'm going to call it LightCommand. So I'm going to change class to interface. And in here, I'm going to define a single void method called execute. Then I'm going to create concrete implementations of this for turning the lights on and off. So I'll start with turning the lights on. So I'm going to create another new class and I'm going to call this one TurnOnLightCommand. And this class is going to implement the LightCommand interface. So first of all, this is going to have a field of type light. So I'm going to say private final Light light. And this is going to be set in a constructor. So I'm going to say public TurnOnLightCommand and pass in a light object. And inside the constructor, I'm going to say this.light equals light. And now I need to override the execute method. So I'm going to say @Override…
Download courses and learn on the go
Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.
Contents
-
-
(Locked)
Understand abstract factories3m 7s
-
(Locked)
Recognize where to use abstract factories3m 53s
-
(Locked)
Create factories for families of objects6m 4s
-
(Locked)
Implement the complete Abstract Factory pattern6m 1s
-
(Locked)
Challenge: The Abstract Factory pattern1m 36s
-
(Locked)
Solution: The Abstract Factory pattern2m 34s
-
(Locked)
-
-
(Locked)
Understand the Chain of Responsibility pattern1m 23s
-
(Locked)
Recognize where to use the Chain of Responsibility pattern2m 35s
-
(Locked)
Implement a successor chain4m 49s
-
(Locked)
Challenge: The Chain of Responsibility pattern1m 48s
-
(Locked)
Solution: The Chain of Responsibility pattern3m 59s
-
(Locked)