auto commit
This commit is contained in:
parent
5cea88d6fa
commit
a8970e7093
@ -815,7 +815,9 @@ public class Singleton {
|
|||||||
|
|
||||||
**1. 问题描述**
|
**1. 问题描述**
|
||||||
|
|
||||||
设计一个遥控器,它有很多按钮,每个按钮可以发起一个命令,让一个家电完成相应操作。有非常多的家电,并且也会增加家电。
|
设计一个遥控器,它有很多按钮,每个按钮可以发起一个命令,让一个家电完成相应操作。
|
||||||
|
|
||||||
|
有非常多的家电,并且之后会增加家电。
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@ -825,16 +827,19 @@ public class Singleton {
|
|||||||
|
|
||||||
将命令封装成对象,以便使用不同的命令来参数化其它对象。
|
将命令封装成对象,以便使用不同的命令来参数化其它对象。
|
||||||
|
|
||||||
**3. 模式类图**
|
**3. 解决方案类图**
|
||||||
|
|
||||||

|
- RemoteControl 是遥控器,它可以为每个按钮设置命令对象,并且调用命令对象的 execute() 方法。
|
||||||
|
- Command 就是命令对象,命令模式正式将各种命令封装成 Commad 对象来实现的。
|
||||||
**4. 解决方案类图**
|
- Light 是命令真正的执行者。可以注意到 LightOnCommand 和 LightOffCommand 类组合了一个 Light 对象,通过组合的方法,就可以将 excute() 方法委托给 Light 对象来执行。
|
||||||
|
- RemoteLoader 是客户端,注意它与 RemoteControl 的区别。因为 RemoteControl 不能主动地调用自身的方法,因此也就不能当成是客户端。客户端好比人,只有人才能去真正去使用遥控器。
|
||||||
Invoker 是遥控器,它可以设置命令,并且调用命令对象的 execute() 方法。Receiver 是电灯,是命令真正的执行者。ConcreteCommand 类组合了一个 Receiver 对象,命令的执行委托给 Receiver 对象来处理,也就是 LightOnCommand 命令的 excute 方法委托给 Light 对象来处理,Light 对象通过调用 on() 方法来完成操作。Invoker 不是 Client 对象,是因为命令的创建不是在 Invoker 中完成的,因此需要额外的 Client 对象来处理这些操作。
|
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
|
**4. 模式类图**
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
**5. 代码实现**
|
**5. 代码实现**
|
||||||
|
|
||||||
```java
|
```java
|
||||||
@ -842,6 +847,7 @@ public interface Command {
|
|||||||
public void execute();
|
public void execute();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Light {
|
public class Light {
|
||||||
|
|
||||||
@ -854,6 +860,7 @@ public class Light {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class LightOnCommand implements Command{
|
public class LightOnCommand implements Command{
|
||||||
Light light;
|
Light light;
|
||||||
@ -868,6 +875,7 @@ public class LightOnCommand implements Command{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/**
|
/**
|
||||||
* 遥控器类
|
* 遥控器类
|
||||||
@ -889,7 +897,11 @@ public class SimpleRemoteControl {
|
|||||||
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
|
/**
|
||||||
|
* 客户端
|
||||||
|
*/
|
||||||
public class RemoteLoader {
|
public class RemoteLoader {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SimpleRemoteControl remote = new SimpleRemoteControl();
|
SimpleRemoteControl remote = new SimpleRemoteControl();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user