【发布时间】:2012-09-09 04:53:36
【问题描述】:
我正在尝试制作纸牌游戏。
我的一些类是:CardModel、CardView;甲板模型,甲板视图。
deck model有一个card model列表,根据MVC,如果我想发送一张卡片到一个deck,我可以将card model添加到deck model,card view会添加到deck view通过事件处理程序。
所以我在 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);
}
}
}
【问题讨论】:
-
请添加相应的代码sn-ps而不是描述问题
标签: java model-view-controller javafx-2 playing-cards