単純なプログラムを書換えよう(Java編)−Swingを使ってみた

View.javaをAWTからSwingにしてみた.修正したのはView.javaだけで,AWTの部品を対応するSwingの部品に置換えただけだ.

// View.java
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Observer;
import java.util.Observable;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;

public class View extends JFrame implements Observer{
  private JLabel value=new JLabel();

  public View(Controller controller){
    Container container=this.getContentPane();
    JPanel buttons=new JPanel();
    JButton inc=new JButton("inc");
    JButton dec=new JButton("dec");
    buttons.add(inc);
    buttons.add(dec);
    inc.addActionListener(controller);
    dec.addActionListener(controller);
    add(buttons,BorderLayout.SOUTH);
    add(value,BorderLayout.CENTER);
    addWindowListener(controller);
    setSize(150,100);
    setVisible(true);
  }
  public void update(Observable obs,Object obj){
    value.setText(Integer.toString(((Model)obs).getValue()));
  }
}
// end

これ以外のプログラムは変わっていない.

// CommandDispatcher.java
import java.lang.reflect.*;
import java.util.Map;
import java.util.Properties;
import java.io.*;

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

  static{
    Properties prop=new Properties();
    try{
      prop.load(new FileInputStream("command.properties"));
    }
    catch(FileNotFoundException e){
      e.printStackTrace();
    }
    catch(IOException e){
      e.printStackTrace();
    }
    commands=(Map)prop;
  }
  private CommandDispatcher(){
  }
  public static CommandDispatcher getInstance(Model model){
    commandDispatcher.model=model;
    return commandDispatcher;
  }

  public Command getCommand(String command){
    Command executeCommand=null;
    try{
      executeCommand=
        (Command)Class.forName(commands.get(command).toString())
          .getConstructor(new Class[]{this.model.getClass()})
            .newInstance(new Object[]{this.model});
    }
    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();
    }
    return executeCommand;
  }
}
// end

// 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

// 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);
    this.model=model;
  }

  public void actionPerformed(ActionEvent actionEvent){
    commandDispatcher.getCommand(actionEvent.getActionCommand()).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

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