我们用命令模式模拟一个带控制开关的小电器。该小电器上有四个开关,两个一组,其中一组负责打开、关闭照明灯,另一组负责打开、关闭小电器上的摄像头。
设计要求
- 设计Cammera类(模拟摄像头)和Light类(模拟照明灯)。
- 设计Machine类(模拟小电器)。
- 要求Machine类创建的对象中含有打开、关闭摄像头以及打开、关闭照明灯的按钮。
设计实现
设计类图如图:

1.接收者
Cammera类和Light类的实例是命令模式的接收者。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| import javax.swing.*; public class Cammera extends JPanel { String name; Icon imageIcon; JLabel label; public Cammera() { label = new JLabel("I am Cammera"); add(label); } public void on() { label.setText("Cammera open"); } public void off() { label.setText("Cammera off"); } }
import javax.swing.*; public class Light extends JPanel{ String name; JLabel label; public Light() { label = new JLabel("I am Light"); add(label); } public void on() { label.setText("Light open"); } public void off() { label.setText("Light off"); } }
|
命令接口
1 2 3 4
| public interface Command { public abstract void execute(); public abstract String getName(); }
|
具体命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| public class OnCammeraCommand implements Command { Cammera cammera; public OnCammeraCommand(Cammera cammera){ this.cammera = cammera; } public void execute() { cammera.on(); } public String getName() { return "打开摄像头"; } }
public class OffCammeraCommand implements Command { Cammera cammera; public OffCammeraCommand(Cammera cammera) { this.cammera = cammera; } public void execute() { cammera.off(); } public String getName() { return "关闭摄像头"; } }
public class OnLightCommand implements Command { Light light; public OnLightCommand(Light light){ this.light = light; } public void execute() { light.on(); } public String getName() { return "打开照明灯"; } }
public class OffLightCommand implements Command { Light light; public OffLightCommand(Light light) { this.light = light; } public void execute() { light.off(); } public String getName() { return "关闭照明灯"; } }
|
请求者
Invoke类的实例含有程序所需要的按钮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import java.awt.*; import javax.swing.*; public class Invoke { JButton button; Command command; public Invoke() { button = new JButton(); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { executeCommand(); }
}); } private void executeCommand() { System.out.println(command.getName()); command.execute(); } public void setCommand(Command command) { this.command = command; button.setText(command.getName()); } public JButton getButton() { return button; }
}
|
小电器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import java.awt.*; import javax.swing.*; public class Machine extends JFrame { Invoke requestOnCammera,requestOffCammera,requestOnLight,requestOffLight; Cammera cammera; Light light; public Machine() { setTitle("小电器"); requestOnCammera = new Invoke(); requestOffCammera = new Invoke(); cammera = new Cammera(); light = new Light(); requestOnCammera.setCommand(new OnCammeraCommand(cammera)); requestOffCammera.setCommand(new OffCammeraCommand(cammera)); requestOnLight = new Invoke(); requestOffLight = new Invoke(); requestOnLight.setCommand(new OnLightCommand(light)); requestOffLight.setCommand(new OffLightCommand(light)); initPosition(); setSize(500,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } private void initPosition() { JPanel ps = new JPanel(); ps.add(requestOnCammera.getButton()); ps.add(requestOffCammera.getButton()); ps.add(requestOnLight.getButton()); ps.add(requestOffLight.getButton()); add(ps, BorderLayout.SOUTH); add(light, BorderLayout.WEST); add(cammera, BorderLayout.EAST); } public static void main(String[] args) { Machine machine = new Machine(); } }
|