Created
December 13, 2012 13:31
-
-
Save anonymous/4276401 to your computer and use it in GitHub Desktop.
MVCってこうですか? わかりません。
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 execute() { | |
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.update(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) { | |
// Q. 最初のViewとModelの作成方法がよくわからない。どれから作るのか? | |
View view = new View(); | |
Model model = new Model("first", view); | |
view.setModel(model); | |
// Viewを表示する | |
view.execute(); | |
Controller cntl = new Controller(model); | |
for (;;) { | |
// 変更をControllerが受け付ける | |
cntl.execute(); | |
// 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
/** | |
* データを保持する | |
*/ | |
public class Model { | |
String name; | |
// Q. Viewを保持してよいのか? | |
View view; | |
public Model(String s, View v) { | |
name = s; | |
view = v; | |
} | |
public Model() { | |
this("none", new View()); | |
} | |
// データの更新 | |
public void update(String s) { | |
name = s; | |
execute(); | |
} | |
// 変更を通知 | |
protected void execute() { | |
view.execute(); | |
} | |
} |
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 { | |
// Q. Modelを保持してよいのか? 相互に参照していることになる。 | |
Model model; | |
public View() { | |
} | |
// 後からモデルを取得 | |
// Q. 初期Viewを作成するための苦肉の策。どうすればよいのか? | |
public void setModel(Model m) { | |
model = m; | |
} | |
// モデルを表示 | |
public void execute() { | |
System.out.println("[" + model.name + "]"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Main
Q. 最初のViewとModelの作成方法がよくわからない。どれから作るのか?
A. どっちでもいいんじゃない?
自分なら、独立しているのはModelだからModelかなぁ。
Swingアプリとかだと、Modelの構築をmainスレッドで行い、完了してからEDTでViewを組み立てる方が楽。
Modelが不完全な場合のViewの振る舞いを実装せずにすむから。
Model
Q. Viewを保持してよいのか?
A. ダメ、絶対。
イベントハンドラを登録するのが吉。最近のGUIだとバインディング機構があるので利用するのもあり。
View
Q. Modelを保持してよいのか? 相互に参照していることになる。
A. Yes, View -> Model の参照はOK、逆はNG
Q. 初期Viewを作成するための苦肉の策。どうすればよいのか?
A. setModelは自然なパターン。
コンストラクタで渡すのも良いけど、SwingとかだとGUIビルダが使えないなどの弊害も出るためデフォルトコンストラクタが必要な場合も多いかな