Example java command template with Runnable class: is there no receiver?

From Examples of GoF Design Patterns in Major Java Libraries , it has been pointed out that

All implementations of java.lang.Runnable are examples of the Command pattern.

According to my understanding of the command template

The client calls Invoker => Invoker calls ConcreteCommand => ConcreteCommand . A receiver that implements the abstract Command method.

Look at this working example.

The UML diagram of the commands in this article is shown below.

enter image description here

Take a look at this code:

public class ThreadCommand{ public static void main(String args[]){ Thread t = new Thread(new MyRunnable()); t.start(); } } class MyRunnable implements Runnable{ public void run(){ System.out.println("Running:"+Thread.currentThread().getName()); } } 
  • ThreadCommand Client
  • Managed Interface Team
  • MyRunnable ConcreteCommmand
  • Invoker theme with the start() method of calling ConcreteCommand implementaiton (which calls the run() method)

Is the Recipient absent here? Or does MyRunnable play the joint role of ConcreteCommand and Receiver ?

+6
source share
4 answers

A receiver is a relic of C / C ++ in which the method that is being called is separated from the object to be called. You can implement this behavior in Java by passing a method using reflection; but, as you might have guessed, the more idiomatic approach of Java is to consider a specific command and receiver as one object.

+3
source

One response was sent here and it was immediately deleted by the author. Reading the answer, I found a solution.

I can simply convert the above example into a UML diagram of a command diagram with one small change.

I can pass the Receiver object to MyRunnable (ConcreteCommand) .

Now I have changed my code as shown below.

 public class ThreadCommand{ public static void main(String args[]){ Receiver r = new AC(); Thread t = new Thread(new MyRunnable(r)); t.start(); } } class MyRunnable implements Runnable{ private Receiver receiver; public MyRunnable(Receiver r){ this.receiver = r; } public void run(){ receiver.execute(); } } interface Receiver{ public void execute(); } class AC implements Receiver{ public void execute(){ System.out.println("I am AC"); } } class Fan implements Receiver{ public void execute(){ System.out.println("I am Fan"); } } 

Output:

  java ThreadCommand I am AC 
+1
source

There will be no receiver ...

 System.out.println("Running:"+Thread.currentThread().getName()); 

calling the run method. Because it gets the action that needs to be performed when Runnable starts.

0
source

I want to give my two cents ...

The original command template separates Command objects from Receiver objects, because the responsibilities of the two sets are different.

Receiver belongs to the business logic of the application. These types must exist outside the template. In a practical case, Receiver was probably already present in the code base, before the commands.

For example, inventing a banking application, the receiver can be represented by the following type:

 public class Account { private final double balance; // Construct omissis public Account deposit(double amount) { // Deposit code } public Account withdraw(double amount) { // Withdraw code } } 

One of the goals of the command design pattern is to provide a single, uniform, and standard way to perform operations on a set of objects (i.e., receivers). They donโ€™t care how to implement real business logic. This will limit the reuse of code that implements business logic.

For this reason, the Command implementation should redirect information to the Receiver . This follows an example.

 public class DepositCommand implements Command { private final Account account; // An instance of Command should reprenset only a single request private final double amount; public DepositCommand(Account account, double amount) { this.account = account; this.amount = amount; } public void execute() { account.deposit(amount); } // Omissis.. } 

In conclusion, imho, the statement contained in the accepted answer is incorrect.

A receiver is a relic of C / C ++ in which the method that is being called is separated from the object to be called.

0
source

All Articles