2021-07-28

命令模式(学习笔记)

  1. 意图

  5. 效果

  1. Command模式将调用操作的对象与知道如何实现该操作的对象解耦(单一职责原则)

  2. 实现撤销和恢复功能

  3. 实现操作的延迟执行

  4. 可以将多个命令装配成一个组合命令。一般来说,组合命令是Composite模式的一个实例

  5. 可以在不修改客户端代码的情况下,在程序中创建新的命令(开闭原则)  

  6. 代码实现  

  commands/Command.java: 抽象基础命令

package command.commands;import command.editor.Editor;/** * @author GaoMing * @date 2021/7/25 - 20:05 */public abstract class Command { public Editor editor; private String backup; Command(Editor editor) {  this.editor = editor; } void backup() {  backup = editor.textField.getText(); } public void undo() {  editor.textField.setText(backup); } public abstract boolean execute();}

  commands/CopyCommand.java: 将所选文字复制到剪贴板

package command.commands;import command.editor.Editor;/** * @author GaoMing * @date 2021/7/25 - 20:06 */public class CopyCommand extends Command { public CopyCommand(Editor editor) {  super(editor); } @Override public boolean execute() {  editor.clipboard = editor.textField.getSelectedText();  return false; }}

  commands/PasteCommand.java: 从剪贴板粘贴文字

package command.commands;import command.editor.Editor;/** * @author GaoMing * @date 2021/7/25 - 20:06 */public class PasteCommand extends Command{ public PasteCommand(Editor editor) {  super(editor); } @Override public boolean execute() {  if (editor.clipboard == null || editor.clipboard.isEmpty()) return false;  backup();  editor.textField.insert(editor.clipboard, editor.textField.getCaretPosition());  return true; }}

  commands/CutCommand.java: 将文字剪切到剪贴板

package command.commands;import command.editor.Editor;/** * @author GaoMing * @date 2021/7/25 - 20:06 */public class CutCommand extends Command{ public CutCommand(Editor editor) {  super(editor); } @Override public boolean execute() {  if (editor.textField.getSelectedText().isEmpty()) return false;  backup();  String source = editor.textField.getText();  editor.clipboard = editor.textField.getSelectedText();  editor.textField.setText(cutString(source));  return true; } private String cutString(String source) {  String start = source.substring(0, editor.textField.getSelectionStart());  String end = source.substring(editor.textField.getSelectionEnd());  return start + end; }}

  commands/CommandHistory.java: 命令历史

package command.commands;import java.util.Stack;/** * @author GaoMing * @date 2021/7/25 - 20:06 */public class CommandHistory { private Stack<Command> history = new Stack<>(); public void push(Command c) {  history.push(c); } public Command pop() {  return history.pop(); } public boolean isEmpty() { return history.isEmpty(); }}

  editor/Editor.java: 文字编辑器的 GUI

package command.editor;import command.commands.*;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;/** * @author GaoMing * @date 2021/7/25 - 20:06 */public class Editor { public JTextArea textField; public String clipboard; private CommandHistory history = new CommandHistory(); public void init() {  JFrame frame = new JFrame("Text editor (type & use buttons, Luke!)");  JPanel content = new JPanel();  frame.setContentPane(content);  frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));  textField = new JTextArea();  textField.setLineWrap(true);  content.add(textField);  JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));  JButton ctrlC = new JButton("Ctrl+C");  JButton ctrlX = new JButton("Ctrl+X");  JButton ctrlV = new JButton("Ctrl+V");  JButton ctrlZ = new JButton("Ctrl+Z");  Editor editor = this;  ctrlC.addActionListener(new ActionListener() {   @Override   public void actionPerformed(ActionEvent e) {    executeCommand(new CopyCommand(editor));   }  });  ctrlX.addActionListener(new ActionListener() {   @Override   public void actionPerformed(ActionEvent e) {    executeCommand(new CutCommand(editor));   }  });  ctrlV.addActionListener(new ActionListener() {   @Override   public void actionPerformed(ActionEvent e) {    executeCommand(new PasteCommand(editor));   }  });  ctrlZ.addActionListener(new ActionListener() {   @Override   public void actionPerformed(ActionEvent e) {    undo();   }  });  buttons.add(ctrlC);  buttons.add(ctrlX);  buttons.add(ctrlV);  buttons.add(ctrlZ);  content.add(buttons);  frame.setSize(450, 200);  frame.setLocationRelativeTo(null);  frame.setVisible(true); } private void executeCommand(Command command) {  if (command.execute()) {   history.push(command);  } } private void undo() {  if (history.is......

原文转载:http://www.shaoqun.com/a/901539.html

跨境电商:https://www.ikjzd.com/

杨帆:https://www.ikjzd.com/w/1648

捷汇:https://www.ikjzd.com/w/419

万色:https://www.ikjzd.com/w/2382


1.意图  5.效果  1.Command模式将调用操作的对象与知道如何实现该操作的对象解耦(单一职责原则)  2.实现撤销和恢复功能  3.实现操作的延迟执行  4.可以将多个命令装配成一个组合命令。一般来说,组合命令是Composite模式的一个实例  5.可以在不修改客户端代码的情况下,在程序中创建新的命令(开闭原则)    6.代码实现    commands/Command.java:抽
声网agora:https://www.ikjzd.com/w/2176
云台山:一线峡的由来 - :http://www.30bags.com/a/407529.html
云台山地理位置介绍 - :http://www.30bags.com/a/407524.html
云台山景区将建11个游览项目:http://www.30bags.com/a/426121.html
云台山两日游怎么玩,云台山两日游路线,云台山两日游攻略 :http://www.30bags.com/a/436677.html
你家老公是怎么上你的 宝贝好想让你㖭我下面:http://lady.shaoqun.com/a/248214.html
口述被三人干了一晚上 两个男人做我的详细过程:http://lady.shaoqun.com/m/a/248079.html
我想㖭你腿间的花 乖乖宝贝让我爽一下:http://www.30bags.com/m/a/249760.html
大卖获亿元投资加持!7月跨境电商融资盘点:https://www.ikjzd.com/articles/146976
跨境电商代运营业务内容有哪些?:https://www.ikjzd.com/articles/146975
亚马逊广告烧钱不出单怎么办?如何提高曝光率:https://www.ikjzd.com/articles/146974
崩了!「解压玩具」侵权已"压垮"133家店铺,卖家快点撤离下架!:https://www.ikjzd.com/articles/146971

No comments:

Post a Comment