我正在嘗試製作紙牌遊戲。在紙牌遊戲中的mvc設計
我有的一些類是:CardModel,CardView; DeckModel,DeckView。
甲板模型有一個卡模型的列表根據MVC,如果我想發送一張卡到甲板上,我可以將卡模型添加到甲板模型,卡視圖將被添加到甲板上由事件處理程序查看。
所以我在DeckModel類中有一個addCard(CardModel m),但是如果我想發送一個事件以將該模型的卡片視圖添加到甲板視圖中,我只需要讓該模型具有對視圖的引用。
所以問題是:如果卡模型和甲板模型必須有一個引用他們的視圖類來做到這一點?如果沒有,如何做得更好?
更新,代碼:
public class DeckModel {
private ArrayList<CardModel> cards;
private ArrayList<EventHandler> actionEventHandlerList;
public void addCard(CardModel card){
cards.add(card);
//processEvent(event x);
//must I pass a event that contain card view here?
}
CardModel getCards(int index){
return cards.get(index);
}
public synchronized void addEventHandler(EventHandler l){
if(actionEventHandlerList == null)
actionEventHandlerList = new ArrayList<EventHandler>();
if(!actionEventHandlerList.contains(l))
actionEventHandlerList.add(l);
}
public synchronized void removeEventHandler(EventHandler l){
if(actionEventHandlerList!= null && actionEventHandlerList.contains(l))
actionEventHandlerList.remove(l);
}
private void processEvent(Event e){
ArrayList list;
synchronized(this){
if(actionEventHandlerList!= null)
list = (ArrayList)actionEventHandlerList.clone();
else
return;
}
for(int i=0; i<actionEventHandlerList.size(); ++i){
actionEventHandlerList.get(i).handle(e);
}
}
}
請添加適當的代碼片段,而不是描述問題 – Chris