- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
来自 Examples of GoF Design Patterns in Java's core libraries问题,有人引用
All implementations of java.lang.Runnable are examples of Command pattern.
根据我对命令模式的理解,
Client 调用 Invoker => Invoker 调用 ConcreteCommand = > ConcreteCommand 调用Receiver 方法,该方法实现了抽象的Command 方法。
看看这个工作example
来自 this article 的命令模式 UML 图如下图所示。
看看这段代码:
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());
}
}
start()
方法调用 ConcreteCommand 实现(调用 run( )
方法)此处是否缺少 Receiver?还是MyRunnable兼具ConcreteCommand和Receiver的作用?
最佳答案
Receiver 是可选的,具体取决于 ConcreteCommand 是否拥有要执行的业务逻辑。从书的第 238 页开始,
A command can have a wide range of abilities. At one extreme it merely defines a binding between a receiver and the actions that carry out the request. At the other extreme it implements everything itself without delegating to a receiver at all.
在最初的问题中,我们看到了一个没有接收者的例子,因为 MyRunnable
拥有要执行的逻辑。在这里的另外两个答案中,我们看到了委托(delegate)给名为 Receiver
和 Account
的显式接收者的示例。
关于带有 Runnable 类 : Is Receiver missing? 的 java 命令模式示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35610215/
好吧,我的问题听起来很困惑,但实际上很简单。我有一个 Runnable 启动另一个 Runnable 实例。所以基本上: 可运行 1 -> 可运行 2 runnable1 是在 runnable2 还
我正在尝试使用我的单例的 Handler.post() 方法从另一个 runnable 运行一个 runnable,但是直到原始 runnable 完成后才运行第二个 runnable。在下面的示例代
为什么下面的第一个例子不起作用? run(R::new); 方法 R.run 未被调用。 run(new R()); 方法 R.run 被调用。 这两个示例都是可编译的。 public class C
为什么下面的代码不起作用?基本上,这是一个更困难的程序的简化版本,在该程序中,我试图创建一个可运行的初始屏幕,其中包含一些选择,然后有链接到不同可运行项的按钮,但这并没有按照我的预期运行。 impor
我有一个带有主选项卡 Activity 的 Android 应用,以及各个选项卡中的多个 Activity 。在我的主要 Activity 的 onCreate() 中,我有一个创建列表的可运行文件,
我正在使用 Mockito 进行测试。我有一个回调接口(interface): interface Callback { void onMessageRetrieved(String mess
这个问题在这里已经有了答案: Does postDelayed cause the message to jump to the front of the queue? (1 个回答) 关闭 7 年
我想将 runnable 发布到 runnable 内的 View 对象,目前我被困在这里。 var runnable = Runnable { if(numLinesToDraw
假设我有一个 ExecutorService,我用它来运行 Job 对象,这是一个扩展 Runnable 的类。我希望我的 Job 对象并行运行,除了让 2 个 Job 对象具有相同的 job.get
下面是该类的源代码。 我想验证如何 shutdownNow()适用于未提交的任务。我在下面的代码中遇到的问题是 shutdownNow()返回 List而不是 List我已经提交了 List包含提交的
我正在尝试找出如何从多线程应用程序中获得最大性能。 我有一个这样创建的线程池: ExecutorService executor = Executors.newFixedThreadPool(8);
这有什么区别?请引用选项1和选项2。因为我遇到了麻烦,因为它们好像是一样的。它们运行正确 Thread ThreadPoolExecutor executor = (ThreadPoolExecuto
当任何命令在任何 ScheduledExecutorService 上以固定速率调度时,它返回 ScheduledFuture 也可以取消。但是“cancel”并不能保证命令在 cancel 返回后仍
因此,我希望在尝试保持轻资源负载的同时同时完成几件事。例如,同时播放声音和更新 GUI。让多个处理程序具有单个可运行对象或单个处理程序具有多个并行运行的可运行对象更好吗? 我知道下面的实现实际上不会同
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Java: “implements Runnable” vs. “extends Thread” 我只是想知道创建自
为什么 Java 的 scheduleWithFixedDelay 使用 Runnable 而不是包装 runnable 的 FutureTask? 这可以很容易地用两个不同的代码示例来展示: Sch
这个问题在这里已经有了答案: What's the difference between Activity.runOnUiThread(runnable action) and Handler.pos
我有如下三个类: 1.线程实例 public class ThreadInstance extends Thread { public ThreadInstance() { }
春天给了我下面的错误,却想不出为什么!。注意:在XML文件中没有对此的Bean定义。。我也提出了这一点,但没有为我的案件找到任何解决方案
一 Runnable 的职责 Runnable 接口非常简单,只定义了一个无参无返回值的 run 方法,源码如下。 @FunctionalInterface public interface Runn
我是一名优秀的程序员,十分优秀!