×

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

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Java Integer shortValue() method

The shortValue() method of Java Integer class returns a short value for this Integer after a narrowing primitive conversion. Syntax public short shortValue () Parameters NA Overrrides: The shortValue () overrides : shortValue in class Number  Return Value This...

1 minute read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

How to run Java Program in Command Prompt

How to run Java Program in Command Prompt In this section, we will learn how to write, save, compile, and execute or run a Java program in the Command Prompt. Note: One...

3 minutes read.

If Condition in Lambda Expression Java

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single-method interface using an expression....

4 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

How to take String Input in Java

There are various ways to take String input in Java. In this section, we are going to discuss how to take String input in Java. There are following ways to...

5 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Difference between this and super in Java

In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this...

3 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.