×

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 Date add Days

In order to operate with the time and the Date in Java, we used the abstract Calendar class. It has several helpful interfaces that enable us to convert dates between...

4 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes 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.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.

Rectangular Numbers in Java

In this tutorial, we will understand the meaning of a rectangular number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview...

3 minutes read.

Jagged Array in Java

Prerequisite We must first understand what Arrays are and Multi-dimensional Arrays are before we can learn about Jagged arrays. Java Arrays: A collection of data types that are similar is called an...

5 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 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.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Operators in Java

There is a good operator environment provided by Java. These operators are divided into four different groups i.e. arithmetic, bitwise, relational, and logical. There are other operators also available to...

7 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.