Java outsourcing

Java outsourcing services provider introducing command pattern for Java development

Posted on Updated on

In this post, java outsourcing experts explain the use of command pattern for app development. Understand command pattern well and implement a call back functionality in java app. Code shared in this post are for reference purpose only

Introduction

In Java, to make the code more reliable we should design our code efficiently that can be done by applying design methodologies such as OOPs, OOAD, design principles and patterns into applications.

Command pattern is a behavioral design pattern which actually help us to implement a call back functionality in Java. I.e. it encapsulate the request and sends to invoker who holds the command that is need to be executed.
GOF states that – “Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations”

command-pattern

Command – Interface to executing a command
Concrete Command – Holds a receiver, Implements command interface and execute the operation on the receiver and it actually creates the link between receiver and the action.
Receiver – Has the knowledge of the command and knows how to perform the operation
Invoker – has a command to execute.
Client – uses invoker thru concrete commands

e.g.
java.lang.Runnable is implemented followed by command pattern
In the real world, we can think of any examples

Buying or Selling a share in ShareMarket

  • Command
  • ConcreteCommand – BuyCommand, SellCommand (executes the command on the receiver)
  • Receiver – Share Market (has the knowledge of shares that companies(ICICI, SBI, TATA, etc.) holding and knows how to perform all the share operations will execute shares)
  • Invoker – Broker or Agent who holds the command
  • Client – creates either buy or sell command basis on their requirement and invokes it thru invoker

Commanding/Operating a TV(Television)

  • Command
  • ConcreteCommand – SwichOn, SwitchOff, ChangeChannel, MuteSound etc.,
  • Receiver – TV (knowledge of how to execute the command operations)
  • Invoker – TVRemote (holds the commands to be executed)
  • Client – Creates the command either switchon or off or change channel and invokes it thru invoker

//Command interface
public interface TVCommand {
void execute();
}

//Concrete Command class SwitchOn
public class SwitchOn implements TVCommand {

// holds a receiver who knows how to perform command operations
private TV tv;

public void execute() {
tv.switchOn();
}
}

//Concrete Command class SwitchOff
public class SwitchOff implements TVCommand {

// holds a receiver who knows how to perform command operations
private TV tv;

public void execute() {
tv.switchOff();
}
}

//Receiver – who knows how to perform operations
public class TV {

public void switchOn() {} // switch on operation

public void switchOff() {} // switch off operation

// more methods or operations like muteSound, changeChannel etc.,
}
//Invoker class TVRemote
public class TVRemote {

// holds a command that needs to be executed
private TVCommand tvCommand;

public void setTVCommand(TVCommand tvCommand) {
this.tvCommand = tvCommand;
}

public void pressButton() {
tvCommand.execute();
}
}

//Client
public class Client {

// creates commands and invoke the commands thru an invoker TVRemote
TVCommand on = new SwitchOn();
TVCommand off = new SwitchOn();

TVRemote tvRemote = new TVRemote();

tvRemote.setCommand(on); // on command
tvRemote.pressButton();

tvRemote.setCommand(off); //off command
tvRemote.pressButton();

}

Java outsourcing services providers have explained the command pattern use for app development. If you think you need assistance of professionals to perform this project, outsource java development services today.

Conclusion

It actually help us to implement a call back functionality and command supports to do undo and redo operations since it is maintaining state with the receiver. And it makes the code more maintainable and adaptable i.e. we can add a new command like change-channel, mute-sound etc. without affecting the existing implementation