-
-
Save torazuka/4318346 to your computer and use it in GitHub Desktop.
頂いたコメントを参考に、MVC(モドキ)を書き直しました。See https://gist.github.com/4276401
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
/** | |
* モデルを更新する | |
*/ | |
public class Controller { | |
Model model; | |
public Controller(Model m) { | |
model = m; | |
} | |
// 操作を受けつけて、モデルを更新する | |
public void handleEvent() { | |
String result = null; | |
BufferedReader bufferedReader = new BufferedReader( | |
new InputStreamReader(System.in)); | |
try { | |
result = readLine(bufferedReader); | |
} catch (IOException e) { | |
System.out.println("入力を読み込めませんでした。"); | |
// TODO: logger.error("入力を読み込めませんでした。"); | |
} | |
if (result == null) { | |
result = ""; | |
} | |
model.service(result); | |
} | |
protected static String readLine(BufferedReader bufferedReader) | |
throws IOException { | |
return bufferedReader.readLine(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
// Modelを作成 | |
Model model = new Model("first"); | |
// 初期Viewを作成 | |
final View view = new View(model); | |
// イベントリスナーを登録: 呼出時にViewを表示する | |
model.add(new EventListener() { | |
@Override | |
public void execute() { | |
view.execute(); | |
} | |
}); | |
// 初期Viewを表示 | |
view.execute(); | |
Controller cntl = new Controller(model); | |
for (;;) { | |
// 変更をControllerが受け付ける | |
cntl.handleEvent(); | |
// Ctrl-Cで終了 | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* データを保持する | |
*/ | |
public class Model { | |
String name; | |
// Viewを直接保持する代わりに、イベントリスナーを保持する | |
List<EventListener> listeners = new ArrayList<>(); | |
public Model(String s) { | |
name = s; | |
} | |
// (Viewによって行われる) データの取得 | |
public String getName() { | |
return name; | |
} | |
// (Controllerによって行われる) データの更新 | |
public void service(String s) { | |
name = s; | |
eventNotify(); | |
} | |
// 変更を通知 | |
protected void eventNotify() { | |
if (listeners != null) { | |
for (EventListener el : listeners) { | |
el.execute(); | |
} | |
} | |
} | |
// EventListenerを登録 | |
public void add(EventListener el) { | |
listeners.add(el); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* モデルを描画する | |
*/ | |
public class View { | |
Model model; | |
public View(Model m) { | |
model = m; | |
} | |
// モデルに更新データをリクエストし、表示する | |
public void execute() { | |
System.out.println("[" + model.getName() + "]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment