単純なプログラムを書換えよう(Java編)−引数のあるコンストラクタをCommandに定義

IncCommand/DecCommandクラスのexecuteメソッドに渡している引数modelをコンストラクタに移します.CommandDispatcherは,引数を指定してIncCommand/DecCommandクラスのインスタンスを生成することになります.修正箇所は以下の通りです.これ以外のModelクラスとViewクラスは変更ありません.

  • Command,IncCommand,DecCommandのexecuteメソッドから引数modelを外す
  • IncCommandとDecCommandに引数のあるコンストラクタを定義
  • CommandDispatcherクラスのgetInstanceメソッドに引数modelを追加
  • CommandDispatcherクラスのgetCommandメソッドを修正
  • CommandDispatcherのメソッドの修正にあわせてControllerクラスを修正

面倒くさくなったのでObserverだけです.EventListenerは省略します.

Javaリフレクションの使用についての考察参照です.

//Command.java
public interface Command{
  void execute();
  //引数をなくす
}
//end

//IncCommand.java
public class IncCommand implements Command{
  private Model model;

  public IncCommand(Model model){
    //引数のあるコンストラクタを定義
    this.model=model;
  }
  public void execute(){
    model.inc();
  }
}
//end

//DecCommand.java
public class DecCommand implements Command{
  private Model model;

  public DecCommand(Model model){
    //引数のあるコンストラクタを定義
    this.model=model;
  }
  public void execute(){
    model.dec();
  }
}
//end

//CommandDispatcher.java
import java.util.Map;
import java.util.HashMap;
import java.lang.reflect.*;
//java.lang.reflect.*をインポート

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

  static{
    commands.put("inc","IncCommand");
    commands.put("dec","DecCommand");
  }
  private CommandDispatcher(){
  }
  public static CommandDispatcher getInstance(Model model){
    //引数にmodelを追加
    commandDispatcher.model=model;
    return commandDispatcher;
  }

  public Command getCommand(String command){
    Command executeCommand=null;
    try{
      Class aClass=Class.forName(commands.get(command).toString()); 
      //生成するクラスを取得.インスタンスはまだできていない
      Class[] classArgs={this.model.getClass()}; 
      //コンストラクタに渡す引数で配列作成.引数はインスタンスではなくクラス
      Constructor aCons=aClass.getConstructor(classArgs); 
      //配列に設定した引数を持つコンストラクタを取得
      Object[] objArgs={this.model}; 
      //コンストラクタに渡す引数で配列作成.次行でコンストラクタを動かすのでインスタンス
      executeCommand=(Command)aCons.newInstance(objArgs); 
      //引数を指定してコンストラクタを起動.インスタンスを取得
    }
    catch(ClassNotFoundException e){
      e.printStackTrace();
    }
    catch(NoSuchMethodException e){
      e.printStackTrace();
    }
    catch(InstantiationException e){
      e.printStackTrace();
    }
    catch(IllegalAccessException e){
      e.printStackTrace();
    }
    catch(InvocationTargetException e){
      e.printStackTrace();
    }
    finally{
      return executeCommand;
    }
  }
}
//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;
import java.util.Observer;

public class Controller extends WindowAdapter implements ActionListener{
  private Model model;
  private CommandDispatcher commandDispatcher;
  
  public Controller(Model model){
    commandDispatcher=CommandDispatcher.getInstance(model);
    //getInstanceメソッドに引数指定
    this.model=model;
  }

  public void actionPerformed(ActionEvent actionEvent){
    commandDispatcher.getCommand(actionEvent.getActionCommand()).execute();
    //executeメソッドから引数削除
  }

  public void windowOpened(WindowEvent windowEvent){
    model.addObserver((Observer)windowEvent.getSource());
    model.setValue(model.getValue());
  }
  public void windowClosing(WindowEvent windowEvent){
    model.deleteObserver((Observer)windowEvent.getSource());
    ((Frame)windowEvent.getSource()).dispose();
    if(model.countObservers()==0){
      System.exit(0);
    }
  }
}
//end

//Model.java
import java.util.Observable;

public class Model extends Observable{
  private int value;
  
  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;
    setChanged();
    notifyObservers();
  }

  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();
  }
}
//end

//View.java
import java.awt.*;
import java.util.Observer;
import java.util.Observable;

public class View extends Frame implements Observer{
  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 update(Observable obs,Object obj){
    value.setText(Integer.toString(((Model)obs).getValue()));
  }
}
//end

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