Re: imcompatible type when converting a List to array
On Tue, 24 Jun 2008 12:08:52 -0700, Lew wrote:
I really want to know. What are the advantages you see to making it a
singleton, and disadvantages otherwise?
Ignoring the use of DefaultListModel for the purposes of this thread,
here's the model:
thufir@arrakis:~/bcit-comp2611-project2$
thufir@arrakis:~/bcit-comp2611-project2$ cat src/a00720398/data/
GuestsModel.java
package a00720398.data;
import javax.swing.*;
import a00720398.util.*;
@SuppressWarnings({"unchecked", "serial"})
public class GuestsModel extends DefaultListModel {
private static final GuestsModel INSTANCE = new GuestsModel();
private GuestsModel() {
java.util.List<Guest> Guests = FileUtil.readGuests();
for (Guest guest : Guests) {
addElement(guest);
}
}
public static GuestsModel getInstance() {
return INSTANCE;
}
}
thufir@arrakis:~/bcit-comp2611-project2$
By making it a Singleton it's impossible to accidentally have two "guest
lists" hanging around, by the definition of Singleton. You wouldn't want
to accidentally have duplicate db tables, would you? Plus, it was extra
points for making it a Singleton ;)
You couldn't accidentally re-read guests and wipe out the data, you'll
just get back INSTANCE. Allegedly, the idea is to modify, in this case,
GuestsModel, and then any changes are supposed to be reflected in the
view.
Had I known x days ago that you could just call addElement() from the
constructor and ?implicitly? add objects to the instance, I would've
saved myself a huge headache. Anyhow, now I know.
Out of curiosity, and on a tangent, if the model extends DefaultListModel
then what would the controller look like? I have:
thufir@arrakis:~/bcit-comp2611-project2$
thufir@arrakis:~/bcit-comp2611-project2$ cat src/a00720398/data/
GuestsController.java
package a00720398.data;
import java.util.*;
import javax.swing.event.*;
public class GuestsController implements ListSelectionListener,
Observer {
public void update(Observable arg0, Object arg1) {
// TODO Auto-generated method stub
}
public void valueChanged(ListSelectionEvent event) {
//Object obj = guestsView.getSelectedValue();
//Guest guest = (Guest) obj;
//displayGuest(guest);
}
}thufir@arrakis:~/bcit-comp2611-project2$
thufir@arrakis:~/bcit-comp2611-project2$
The purpose of GuestsController is to sync the view with the model?
Controller
Processes and responds to events, typically user actions, and may
invoke changes on the model.
http://en.wikipedia.org/wiki/Model-view-controller
-Thufir