- 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();
}
}
推薦閱讀
- C語言程序設(shè)計案例教程
- Java程序設(shè)計(慕課版)
- Expert C++
- Drupal 8 Blueprints
- Monkey Game Development:Beginner's Guide
- Functional Programming in JavaScript
- Silverlight魔幻銀燈
- 差分進化算法及其高維多目標優(yōu)化應(yīng)用
- 碼上行動:用ChatGPT學會Python編程
- C#應(yīng)用程序設(shè)計教程
- Mastering C++ Multithreading
- Learning Modular Java Programming
- Building Serverless Web Applications
- JSP程序設(shè)計與案例實戰(zhàn)(慕課版)
- Python Social Media Analytics