From Examples of GoF Design Patterns in Java's core libraries question, it was quoted that
"All implementations of java.lang.Runnable" are examples of Command pattern.
As per my understanding of Command pattern,
Client calls Invoker => Invoker calls ConcreteCommand => ConcreteCommand calls Receiver method, which implements abstract Command method.
Have a look at this working example
Command pattern UML diagram from this article is shown as below.
Have 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 is
Client - Runnable interface is
Command - MyRunnable is
ConcreteCommmand - Thread is
Invokerwith start() method callingConcreteCommandimplementaiton ( which callsrun()method)
Is Receiver missing here? Or MyRunnable plays combined role of ConcreteCommand and Receiver?
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire