プログラミングの初心者として、私は壁にぶつかったときにいつもバグしています。 現在、壁の1つは、共存するオブジェクトです。Java MVC - 私がそれを得るように感じていない
あなたは私の質問履歴に見ることができるように私は現在、私はMVCパターンを呼び出す何かを実装している、ブラックベリーのアプリケーションに取り組んでいますが、それは、私はあることを意味していると思い正確に何ではありません。
あなたは、あなたがこのようなグラフィック抄録に見える初心者プログラマを見て、あなたはその背後にあるアイデアを得ます。しかし、それを実装することは別のことです。
alt text http://www.ibm.com/developerworks/wireless/library/wi-arch6/theoretical.gif
、私はあなたにいくつかのブラックベリーの特定のものが含まれている私のいくつかのコードを、示しています:)読むのをやめませんが、私がやっているかを確認する必要があります。私のアプリケーション
public class ContactManager extends UiApplication
{
private static ContactManagerMainScreenModel MainScreenModel = new ContactManagerMainScreenModel();
private static ContactManagerMainScreen MainScreenView = null;
public static void main(String[] args)
{
new ContactManager().enterEventDispatcher();
}
public ContactManager()
{
MainScreenView = new ContactManagerMainScreen(MainScreenModel);
// Displays the Splashscreen then opens the Mainscreen
new SplashScreen(UiApplication.getUiApplication(), MainScreenView);
}
}
ため
メインエントリポイントMainscreenモデル
public class ContactManagerMainScreenModel
{
ContactManagerMainScreen v;
// Loading Local Storage
LocalContactStorage LocalContactStorage = new LocalContactStorage();
// Define Data List
private Vector vContacts_Favorites;
public void register(ContactManagerMainScreen v)
{
this.v = v;
}
// Retrieve Favorite Contacts from Persistant Storage
public Vector getFavoriteContactsFromLocalStorage()
{
vContacts_Favorites = LocalContactStorage.getFavoriteContactsFromLocalStorage();
return vContacts_Favorites;
}
// Put Retrieve Favorite Contacts from Persistant Storage
public void saveFavoriteContactsToLocalStorage()
{
LocalContactStorage.saveFavoriteContactsToLocalStorage(vContacts_Favorites);
}
}
MainScreenController
public class ContactManagerMainScreenController
{
private ContactManagerMainScreenModel _model = null;
private ContactManagerMainScreen _view = null;
public ContactManagerMainScreenController(ContactManagerMainScreen view, ContactManagerMainScreenModel model)
{
this._model = model;
this._view = view;
}
public void HideFavoriteList()
{
if(this._view._ContactList.getManager() != null)
{
this._view._ContactList.getManager().delete(this._view._ContactList);
} else
{
this._view._bottom_box.add(this._view._ContactList);
}
}
}
まだありますか?わかりました...ここ
私の問題は、私はポップアップボックスが、いろいろ書いたり他のものを隠して表示するように、UI要素を変更するには、コントローラを使用したいということ、です。
しかし、これらすべてのUIエレメントがビュー(ここではContactManagerMainScreen)で定義されているので、コントローラにビューへの参照を与える必要があります。これは私の共存する悲惨なものにつながります。
ビューが宣言される前にコントローラを作成できません。コントローラがUIElementsを変更できるようにする必要がなければ、(グラフィックスに示されているように)問題はありません。私が今やっている何を
は、ビューが理にかなっているコントローラ
controller = new ContactManagerMainScreenController(this , model);
んを作成することですか?私はパターンを理解したいので、自分のコードゴミや好きなものを呼んでください:)ただ何かを学びたいだけです。
P.S.私は私の悪い英語のためのあなたの許しを請う:)
+1私は全く同意します。 – neuro
+1これはまた、私の経験と結びついています。 – Zarkonnen
(誰でも*ビューとコントローラーをちゃんと分離する方法があれば、私は非常に興味があります) – Zarkonnen