単純なプログラムを書換えよう(Java編)−EventListenerとCommandとSingleton

EventListenerにもCommandDispatcherクラスを導入しました.

//Command.java
public interface Command{
  void execute(Object o);
}
//end

//IncCommand.java
public class IncCommand implements Command{
  public void execute(Object model){
    ((Model)model).inc();
  }
}
//end

//DecCommand.java
public class DecCommand implements Command{
  public void execute(Object model){
    ((Model)model).dec();
  }
}
//end

//CommandDispatcher.java
import java.util.Map;
import java.util.HashMap;

public class CommandDispatcher{
  private static Map commands=new HashMap();
  private static CommandDispatcher commandDispatcher=new CommandDispatcher();

  static{ 
    //実行するコマンド名と生成するCommandクラスを対応付ける
    commands.put("inc","IncCommand");
    commands.put("dec","DecCommand");
  }
  private CommandDispatcher(){
    //Singletonなのでコンストラクタは外部から使えなくする
  }
  public static CommandDispatcher getInstance(){
    return commandDispatcher;
  }
  public Command getCommand(String command){
    Command executeCommand=null;
    try{
      executeCommand=(Command)Class.forName(commands.get(command).toString()).newInstance();
    }
    catch(ClassNotFoundException e){
      e.printStackTrace();
    }
    catch(InstantiationException e){
      e.printStackTrace();
    }
    catch(IllegalAccessException e){
      e.printStackTrace();
    }
    finally{
      return executeCommand;
    }
  }
}
//end

//ValueEvent.java
import java.util.EventObject;

public class ValueEvent extends EventObject{
  public ValueEvent(Model model){
    super(model);
  }
  public int getValue(){
    return ((Model)source).getValue();
  }
}
//end

//ValueEvent.java
import java.util.EventListener;

public interface ValueListener extends EventListener{
  public void valueChanged(ValueEvent valueEvent);
}
//end

//Controller.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;
import java.awt.Frame;

public class Controller extends WindowAdapter implements ActionListener{
  private Model model;
  private CommandDispatcher commandDispatcher=CommandDispatcher.getInstance();
  
  public Controller(Model model){
    this.model=model;
  }

  public void actionPerformed(ActionEvent actionEvent){ 
    commandDispatcher.getCommand(actionEvent.getActionCommand()).execute(model);
    //実行したコマンドをif文で判定する必要がなくなった
  }

  public void windowOpened(WindowEvent windowEvent){
    model.addValueListener((ValueListener)windowEvent.getSource());
    model.setValue(model.getValue());
  }
  public void windowClosing(WindowEvent windowEvent){
    model.removeValueListener((ValueListener)windowEvent.getSource());
    ((Frame)windowEvent.getSource()).dispose();
    if(model.countValueListeners()==0){
      System.exit(0);
    }
  }
}
//end

//Model.java
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;

public class Model{
  private int value;
  private Set listeners=new HashSet();
  
  public Model(){
    this(0);
  }
  public Model(int value){
    setValue(value);
  }

  public final int getValue(){
    return value;
  }
  public final void setValue(int value){
    this.value=value;
    fireValueChanged();
  }

  public int inc(){
    return inc(1);
  }
  public int inc(int value){
    setValue(getValue()+value);
    return getValue();
  }
  public int dec(){
    return dec(1);
  }
  public int dec(int value){
    setValue(getValue()-value);
    return getValue();
  }

  public int countValueListeners(){
    return listeners.size();
  }
  public void addValueListener(ValueListener valueListener){
    if(!listeners.contains(valueListener)){
      listeners.add(valueListener);
    }
  }
  public void removeValueListener(ValueListener valueListener){
    listeners.remove(valueListener);
  }
  public void fireValueChanged(){
    ValueEvent event=new ValueEvent(this);
    for(Iterator i=listeners.iterator();i.hasNext();){
      ((ValueListener)i.next()).valueChanged(event);
    }
  }
}
//end

//View.java
import java.awt.*;

public class View extends Frame implements ValueListener{
  private Label value=new Label();

  public View(Controller controller){
    Panel buttons=new Panel();
    Button inc=new Button("inc");
    Button dec=new Button("dec");
    buttons.add(inc);
    buttons.add(dec);
    inc.addActionListener(controller);
    dec.addActionListener(controller);
    add(buttons,BorderLayout.SOUTH);
    add(value,BorderLayout.CENTER);
    addWindowListener(controller);
    pack();
    show();
  }
  public void valueChanged(ValueEvent valueEvent){
    value.setText(Integer.toString(valueEvent.getValue()));
  }
}
//end

//Main.java
public class Main{
  public static void main(String[] args){
    new View(new Controller(new Model()));
  }
}
//end