- Design Patterns and Best Practices in Java
- Kamalmeet Singh Adrian Ianculescu LUCIAN PAUL TORJE
- 165字
- 2021-06-25 20:52:37
Applicability and examples
The applicability and examples of the command pattern are as follows:
- Undo/redo operations: The command pattern allows us to store the command object in a queue. This way, we can implement undo and redo operations.
- Composite commands: Complex commands can be composed of simple commands using the composite pattern, and are run in a sequential order. In this way, we can build macros in an object-oriented-design manner.
- The asynchronous method invocation: The command pattern is used in multithreading applications. Command objects can be executed in the background in separate threads. The java.lang.Runnable is a command interface.
In the following code, the runnable interface acts as a command interface, and is implemented by RunnableThread:
class RunnableThread implements Runnable
{
public void run()
{
// the command implementation code
}
}
The client invokes the command to start a new thread:
public class ClientThread
{
public static void main(String a[])
{
RunnableThread mrt = new RunnableThread();
Thread t = new Thread(mrt);
t.start();
}
}
推薦閱讀
- PHP程序設(shè)計(jì)(慕課版)
- Web Application Development with R Using Shiny(Second Edition)
- Windows Server 2012 Unified Remote Access Planning and Deployment
- Microsoft System Center Orchestrator 2012 R2 Essentials
- Oracle JDeveloper 11gR2 Cookbook
- MATLAB 2020從入門到精通
- Mastering JavaScript Design Patterns(Second Edition)
- Swift語(yǔ)言實(shí)戰(zhàn)精講
- Linux:Embedded Development
- Python機(jī)器學(xué)習(xí)算法與應(yīng)用
- 你好!Java
- 信息學(xué)奧林匹克競(jìng)賽初賽精講精練
- TypeScript High Performance
- 編寫高質(zhì)量代碼之Java(套裝共2冊(cè))
- C++ Windows Programming