1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Design Patterns in Java Projects

Command

Contents

keyboard_tab
Design Patterns
1
Design Patterns
PREVIEW2m 26s
2
Singleton Pattern
PREVIEW3m 13s
9
Decorator
7m 48s
10
Flyweight
9m 20s
11
Proxy
7m 36s
12
Facade
6m 5s
14
Command
11m 44s
15
Iterator
8m 24s
16
Visitor
9m 4s
17
Mediator
11m 38s
18
Memento
9m 48s
19
Observer
10m 40s
20
Strategy
8m 27s
21
State
10m 27s
22
Template
7m 56s
23
MVC
10m 27s
26
DAO
10m 27s
27

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
4h 56m
Students
19
Ratings
5/5
starstarstarstarstar
Description

This course takes an in-depth look at how to use design patterns in your Java projects. We will then run through some example questions of what you could expect from the Oracle Certified Java EE exam.

Learning Objectives

  • Understand what design patterns are
  • Learn about the myriad of design patterns that you can use in your projects

Intended Audience

This course is intended for anyone who already has basic knowledge of Java and now wants to learn about Java EE 6.

Prerequisites

Basic knowledge of Java programming.

 

Transcript

Hello dear friends. In this video, we will examine command pattern. So, let's begin. Command is a behavioral design pattern that turns a request into a standalone object that contains all information about the request. This transformation lets you pass requests as a method argument, delay or queue a request execution, and support undoable operations. Command pattern is a data-driven design pattern. A request is wrapped under an object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and passes the command to the corresponding object which executes the command. It's also known as action or transaction. Let's examine the working structure of this pattern on the UML diagram. The command object knows about the receiver and invokes methods of the receiver by supplying parameters. Values for parameters of the receiver methods are stored in the command object. The receiver objects perform the job when the executed method of command gets called. We pass this command object to an invoker object to perform the execute method.

Invoker object executes the method of the command object and passes the required parameters to it. The invoker can take different command objects so the client deals only with the invoker to perform operations on different objects. These objects call receiver objects. The command object collects all the details of requests and performs operations on the receiver. The command design pattern supports undoable operations as well and we can have an unexecute kind of method to return the state of the object back to its original state. So, the pattern makes it super easy to perform a variety of jobs requests in a simple way by using invoker object.

The invoker may also do bookkeeping for the command objects. Different command objects may have different receiver objects to act on. The execute method defines and performs all the operations in the sequence to complete the job. The client application knows when and in which order to execute the commands. The only drawback of this pattern is we end up creating many command classes. With this working structure, we can say the command pattern separates the object that invokes the operation from the object that usually performs the operation. It also simplifies adding new commands because existing classes remained unchanged. So, in which cases would it be more appropriate to choose the structure? 

It's more useful to choose this structure when you need to parameterize objects according to an action to be performed or when you want to create queue or execute requests at different times. I think it's time to create a project to help learn how to implement this pattern. Create a new project and name it Command. The most commonly used example for this pattern is file operations, but there are a lot of examples of code for file operations online. So, I want to make something else. In our example, let's turn on and off the television and the air conditioner. First, we need an interface. As I command interface which will be implemented by the exact commands, add an execute method. Now we need classes for Television and AirConditioner. Start with Television. We need on and off methods. Done. Add AirConditioner. Add methods for on and off. Now, for each method we have to define a new class that implements from command, TurnOnTelevision. Add Television.

Add constructor. In constructor, we have to add a super class. Now we have to overwrite execute method. Let's add for TurnOffTelevision. This will be the same with turn on, only we change this text, so if you want, you can copy from on method. Now for AirConditioner, we have to define these two classes also. First, start with turn on. Add AirConditioner. Create constructor. Add super class. Overwrite execute method. Now, we will add for turn of the AirConditioner. Done. Now we need remote control. Add interface. We need a method to set command. Also, we need buttonpressed method. Now we have finished structure, let's use in main. First, I want to add two televisions and two air conditioners for kitchen and bedroom. Now create an instance from remote control. Now we will set commands one by one. First, television on bedroom. Turn on, press button. Now, turn on television in living room. Press button, go on. Done. Now when we run the program, we can see the working order of these commands run. Look, we can see the commands are working. Great. I think that's enough for the command design pattern. In the next video, we will look at the iterator design pattern. See you in the next video.

 

About the Author
Students
413
Courses
35
Learning Paths
2

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics