`

体验模式的乐趣(一)—— Command模式

阅读更多

        模式是什么?说实在的,对于模式,我了解得很少。但是,我知道,模式是用来解决软件设计问题的,是用来增加软件设计的灵活性和可维护性的,总之,它应该是程序员经过大量开发工作之后,对于解决问题的经验总结;模式不是用来生搬硬套的,也不是拿来故弄玄虚的,它是用来解决问题,从而让我们更加享受编码的乐趣。我无意直接去讲解什么模式,因为我还没有那样的实力,我能够讲的就只有模式给我带来的顿悟了。咦,怎么文不对题了呢?呵呵,有了顿悟,难道还不是莫大的乐趣吗?闲话少说,言归正传。
        首先,让我们先来看一段代码:

public class MainFrame extends JFrame {
  JPanel panel;

  
public MainFrame() {
  panel 
= new JPanel();
  panel.setLayout(
new FlowLayout());
  JButton blueButton 
= new JButton("Blue");
  blueButton.addActionListener(
new BlueColorActionListener());
  JButton redButton 
= new JButton("Red");
  redButton.addActionListener(
new RedColorActionListener());
  panel.add(blueButton);
  panel.add(redButton);
  getContentPane().add(panel);
  setSize(
300100);
  setVisible(
true);
 }

 
 
class BlueColorActionListener implements ActionListener {
  
public void actionPerformed(ActionEvent event{
   panel.setBackground(Color.blue);
  }

 }

 
 
class RedColorActionListener implements ActionListener {
  
public void actionPerformed(ActionEvent event{
   panel.setBackground(Color.red);
  }

 }

 
 
public static void main(String[] args) {
  MainFrame frame 
= new MainFrame();
 }

}

        如果你有写过Java GUI的程序,一定会对以上的代码很熟悉。这段小代码实现的功能也很简单,就是通过点击按钮去改变窗口的背景颜色。大家在看这段代码的时候,有没有不爽的感觉呢?是的,就是这里了。为了使得窗口上的按钮能够响应鼠标或者是键盘的事件,我们都必须为每个按钮增加相应的ActionListener。如果每个button对应Click事件有迥异的处理,那么构建不同的ActionListener也是无可厚非的,但是,如果按下button,所做的事情很类似呢?譬如以上代码中,两个按钮的功能就很类似,一个是让背景变成红色,一个则是蓝色而已。那么,我们是不是应该将这些共同点提取出来,以减少代码的重复呢?接着就是第二个版本的程序了,使用CommonActionListener去替代原有的两个ActionListener:

class CommonActionListener implements ActionListener {
  
public void actionPerformed(ActionEvent event{
   JButton button 
= (JButton)event.getSource();
   
if (button.getText().equals("Blue"))
    panel.setBackground(Color.blue);
   
else if (button.getText().equals("Red"))
    panel.setBackground(Color.red); 
  }

}

        这样的改动之后,最明显的提高之处就是减少了一个Inner Class,让代码更加集中了,同时复杂性也转嫁到了一个class中。大家可以想象,随着button的增加,if...else的数量也随之增加,如果又出现了一个不同功能的button,增加新的ActionListener也是必不可少的。这个CommonActionListener根本就不Common。到底怎么样才能让它变成真正Common呢?好,最重要的问题终于提出来了。

        为了解决这个问题或许有不同的途径,其中的一条就是将真正的事件处理代码从ActionListener中剥离出来,同时利用强大的多态来避免条件的判断。既然要使用到多态了,那么我们就要从类型上去区分功能不同的button了,同时让它们对外具有相同的API。下面就是最终版本的程序了:
public interface Command {
    
public void execute();
}
                                                      (Command.java)
class BlueButton extends JButton implements Command {
    JPanel panel;
    
public BlueButton(String text, JPanel panel) {
        super(text);
        
this.panel = panel;    
        
this.addActionListener(new CommonActionListener());
    }

    
public void execute() {
        panel.setBackground(Color.blue);
    }

}


class RedButton extends JButton implements Command {
    JPanel panel;
    
public RedButton(String text, JPanel panel) {
        super(text);
        
this.panel = panel;    
        
this.addActionListener(new CommonActionListener());
    }

    
public void execute() {
        panel.setBackground(Color.red);
    }

}
                                                    (Button.java)
public class CommonActionListener implements ActionListener {
    
public void actionPerformed(ActionEvent event{
        Command command 
= (Command)event.getSource();
        command.execute();
    }

}
                                             (CommonActionListener.java)
public class MainFrame extends JFrame {
    
public MainFrame() {
      JPanel panel 
= new JPanel();
      panel.setLayout(
new FlowLayout());
      JButton blueButton 
= new BlueButton("Blue", panel);
      JButton redButton 
= new RedButton("Red", panel);
      panel.add(blueButton);
      panel.add(redButton);
      getContentPane().add(panel);
      setSize(
300100);
      setVisible(
true);
    }
    
    
    
public static void main(String[] args) {
      MainFrame frame 
= new MainFrame();
    }

}

                                                        (MainFrame.java)

        以上的代码正是使用Command模式,对于ActionListener而言,它只是知道调用execute方法,而这个方法对于每一个button都是适用的,就像是命令(Command)一样,而真正的处理则放到了每个特定的button中。就像一位指挥官对着士兵们喊出Action,接着士兵们就去行动了,而指挥官并不需要知道士兵们怎么去做事情,因为指挥官知道士兵们都能做好事情,这些士兵都是服从命令的(实现了Command接口)。需要说明的一点是,在这里由于两个button的功能比较类似,这样组织代码所带来的好处并不是非常突出,大家可以考虑一下,如果button的功能截然不同的情况。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics