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
