auto commit

This commit is contained in:
CyC2018 2018-06-04 22:15:58 +08:00
parent 44244bf63a
commit 3d1ecb3803
3 changed files with 114 additions and 1 deletions

View File

@ -473,7 +473,118 @@ public class Client {
### 意图 ### 意图
将命令封装进对象中;允许使用命令对象对客户对象进行参数化;允许将命令对象存放到队列中。 将命令封装成对象中,以便使用命令来参数化其它对象,或者将命令对象放入队列中进行排队,或者将命令对象的操作记录到日志中,以及支持可撤销的操作。
### 类图
- Command命令
- Receiver命令接收者也就是命令真正的执行者
- Invoker通过它来调用命令
- Client可以设置命令与命令的接收者
<div align="center"> <img src="../pics//ae1b27b8-bc13-42e7-ac12-a2242e125499.png"/> </div><br>
### 实现
设计一个要控制,可以控制电灯开关。
<div align="center"> <img src="../pics//e6bded8e-41a0-489a-88a6-638e88ab7666.jpg"/> </div><br>
```java
public interface Command {
void execute();
}
```
```java
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
}
```
```java
public class LightOffCommand implements Command {
Light light;
public LightOffCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.off();
}
}
```
```java
public class Light {
public void on() {
System.out.println("Light is on!");
}
public void off() {
System.out.println("Light is off!");
}
}
```
```java
/**
* 遥控器
*/
public class Invoker {
private Command[] onCommands;
private Command[] offCommands;
private final int slotNum = 7;
public Invoker() {
this.onCommands = new Command[slotNum];
this.offCommands = new Command[slotNum];
}
public void setOnCommand(Command command, int slot) {
onCommands[slot] = command;
}
public void setOffCommand(Command command, int slot) {
offCommands[slot] = command;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
}
}
```
```java
public class Client {
public static void main(String[] args) {
Invoker invoker = new Invoker();
Light light = new Light();
Command lightOnCommand = new LightOnCommand(light);
Command lightOffCommand = new LightOffCommand(light);
invoker.setOnCommand(lightOnCommand, 0);
invoker.setOffCommand(lightOffCommand, 0);
invoker.onButtonWasPushed(0);
invoker.offButtonWasPushed(0);
}
}
```
### JDK ### JDK
@ -1336,6 +1447,8 @@ public class Client {
### 实现 ### 实现
观看电影需要操作很多电器,使用外观模式可以实现一键看电影功能。
```java ```java
public class SubSystem { public class SubSystem {
public void turnOnTV() { public void turnOnTV() {

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB