単純なプログラムを書換えよう(Java編)−Applet

ObserverとCommandとSingletonを使ったプログラムをAppletにしてみました.手を入れたのはView.javaとController.javaで,それ以外は修正不要です.また,Appletなのでmain関数を記述したMain.javaはファイル自体が不要です.

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

public class View extends Applet implements Observer{
  private Label value=new Label();

  public void init(){
    Model model=new Model();
    Controller controller=new Controller(model);

    setLayout(new BorderLayout());
    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);
    model.addObserver((Observer)this);
    model.setValue(model.getValue());
  }
  public void update(Observable obs,Object obj){
    value.setText(Integer.toString(((Model)obs).getValue()));
  }
}
//end

//Controller.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Controller 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);
  }
}
//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

//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{
    commands.put("inc","IncCommand");
    commands.put("dec","DecCommand");
  }
  private CommandDispatcher(){
  }
  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

appletviewerで表示させるため,以下のhtmlファイル(view.html)も作成しました.

<html>
<body>
  <object code="View.class" width="100" height="100"></object>
</body>
</html>