- 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();
}
}
推薦閱讀
- The Supervised Learning Workshop
- OpenCV實例精解
- SQL Server 2012數據庫技術及應用(微課版·第5版)
- Unity 2020 Mobile Game Development
- 基于Java技術的Web應用開發
- 深入理解Java7:核心技術與最佳實踐
- Oracle數據庫從入門到運維實戰
- Scratch 3游戲與人工智能編程完全自學教程
- 零基礎學MQL:基于EA的自動化交易編程
- ArcGIS By Example
- Python機器學習算法: 原理、實現與案例
- C/C++數據結構與算法速學速用大辭典
- Kubernetes進階實戰
- 算法精解:C語言描述
- VMware vRealize Orchestrator Essentials