×

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure or SQL statement. The course additionally includes a connection object of its own. You can create a connection object directly in the Command class or pass one already built. In either case, no extra connections to the server are opened because the Command class only keeps track of a connection object. All standard ADO.NET providers offer the Command class, which almost invariably contains a SQL statement or a call to a stored procedure that can be used to access a data source. When used with a DataAdapter, command objects can alter the structure of a database, retrieve rows, directly insert, delete, or modify records, compute totals and averages, and fill a disconnected DataSet.

Every Command must, at the very least, define a few critical properties, such as CommandText and CommandType, which it utilises to communicate with the data source. SqlCommand and OleDbCommand are only two of the provider-specific variations of the Command class that are available.

The Command class is a design element containing an action’s semantic details. This item does not have the behaviour that the command activates. This means that nothing happens when the order is activated; information about the "command" is contained in the base.

Use of Command class

You may use a programmable remote to turn on and off various home appliances, including the AC, music, and lighting.

However, we must remember that some gadgets, like stereos, require multiple steps to turn on, such as adjusting the volume and disc. We can also change the function of a button. We are coding for implementation rather than interface when we use simple if-else statements.

So what we're aiming for is a design that offers loose coupling and remote control that doesn't know anything about a specific item.

Advantages

  • Enables us to add new instructions without modifying the existing code, which makes our code adaptable.
  • Decreases the coupling between the command invoker and the command recipient.

Disadvantages

  • More classes should be used for each different action or command.

Program for Command class

// Program to demonstrate command class in Java

// Importing required packages for the command program
import java.io.*;
import java.util.*;
// Creating an interface for command operations
interface Command
{
public void execute ( ) ;
}
// Classes  related to Light
class Lights
{
// Method for function when lights are on
public void on ( )
{
System . out . println ( " Light is on ") ;
}
// Method for function when lights are off
public void off ( )
{
System . out . println ( " Light is off ") ;
}
}
// using the interface to implement command interface for light on class 
class LightOn implements Command
{
Lights l1 ;
// function for light on
public LightOn ( Lights l1 )
{
this . l1 = l1 ;
}
public void execute ( )
{
l1 . on ( ) ;
}
}
// using the interface to implement command interface for light off class 
class LightOff implements Command
{
Lights l1 ;
// function for light off
public LightOff (Lights l1)
{
this . l1 = l1 ;
}
public void execute ( )
{
l1 . off ( ) ;
}
}
class Stereo1
{
public void on ( )
{
System . out . println ( " Stereo is set to on " ) ;
}
public void off ( )
{
System . out . println ( " Stereo is set to off " ) ;
}
public void setCD ( )
{
System . out . println ( " Stereo is set " + "for the  CD input");
}
public void setDVD ( )
{
System . out . println ( " Stereo is set " + " for the DVD input " ) ;
}
//  To alter  volume
public void setVolume ( int volume )
{
System . out . println ( " Stereo volume set " + " to " + volume ) ;
}
}
class StereoOffCommand implements Command
{
Stereo1 s2 ;
public StereoOffCommand ( Stereo1 stereo )
{
this . s2 = stereo ;
}
public void execute ( )
{
s2 . off ( ) ;
}
}
class StereoOnWithCDCommand implements Command
{
Stereo1 s1 ;
public StereoOnWithCDCommand ( Stereo1 stereo )
{
this . s1 = stereo ;
}
public void execute ( )
{
s1 . on ( ) ;
s1 . setCD ( ) ;
s1 . setVolume ( 11 ) ;
}
}
// class control, which contains all commands
class Control
{
Command sl ; 
public Control ( )
{
}
public void setCommand ( Command command )
{
sl = command ;
}
public void buttonWasPressed()
{
sl . execute ( ) ;
}
}
// Main class, which contains the main function
class CommandClass
{
public static void main ( String [ ] args )
{
Control remote = new Control ( ) ;
Lights l1 = new Lights();
Stereo1 s1 = new Stereo1();
remote . setCommand ( new LightOn ( l1 ) ) ;
remote . buttonWasPressed ( ) ;
remote . setCommand ( new StereoOnWithCDCommand ( s1 ) ) ;
remote . buttonWasPressed ( ) ;
remote . setCommand ( new StereoOffCommand ( s1 ) ) ;
remote . buttonWasPressed ( ) ;
remote . setCommand ( new LightOff ( l1 ) ) ;
}
}

Output

Command Class in Java

Related Topics

Differences between Set and List in Java

Set in Java: The Java. util package contains an interface called the set. The set interface expands the Collection interface. A collection interface is an unordered collection of List where duplicates...

6 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

Majority Element in Java

It's an extremely intriguing question that is commonly asked in job interviews at prestigious IT firms like The Google, Amazon, TCS, and The Accenture, etc. By figuring out the solution, one may...

10 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Tokens

Classes and methods are included in the Java program. The procedures also provide the expressions and statements required to finish a specific operation. Tokens make up the sentences and expressions...

4 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Java String replaceAll() method

Java String replaceAll() method returns a String replacing all the sequence of characters matching regular expression i.e regex and replacement string. Syntax: public String replaceAll(String regex, String replacement) Parameters: regex : regular expression replacement :...

1 minute read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Display List of TimeZone with GMT and UTC in Java

It is vital to establish the right TimeZone in Java code when working with dates for Daylight Saving Time. In this part, we will present the time zones with GMT. TimeZone Those...

5 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.