×

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 array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

Application of Array in Java

In this article we are going to acknowledge about what the array is, types of arrays and their applications. What is an array? An array is often a set of interrelated elements...

4 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

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.

Model Class in Java

In this section, we will be acknowledged about the model class in Java, its purpose and its uses. Also, we will learn how is this created and leveraged in java. Model...

4 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

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

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.