Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this MVC

 
Fred Barnes
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class View extends JFrame implements Observer {
private JLabel label;
private JTextField field;
public View() {
label = new JLabel();
field = new JTextField(10);
JButton change = new JButton("Change!");
change.addActionListener(new Controller(this));
JPanel panel = new JPanel();
panel.add(label);
panel.add(field);
panel.add(change);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
View v = new View();
}
/**
* @see java.util.Observer#update(java.util.Observable, java.lang.Object)
*/
public void update(Observable arg0, Object arg1) {
label.setText(((Model) arg0).getValue());
}
public String getValue() {
return field.getText();
}
}
public class Controller implements ActionListener {
private View view;
private Model model;
public Controller(View view) {
this.view = view;
model = new Model();
model.addObserver(view);
model.setValue("Fred");
}
public void actionPerformed(ActionEvent ae) {
model.setValue(view.getValue());
}
}
 
Fred Barnes
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I made a mistake! Here follws the source...
public class View extends JFrame implements Observer {
private JLabel label;
private JTextField field;
public View() {
label = new JLabel();
field = new JTextField(10);
JButton change = new JButton("Change!");
change.addActionListener(new Controller(this));
JPanel panel = new JPanel();
panel.add(label);
panel.add(field);
panel.add(change);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
this.getContentPane().add(panel);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
View v = new View();
}
public void update(Observable arg0, Object arg1) {
label.setText(((Model) arg0).getValue());
}
public String getValue() {
return field.getText();
}
}
public class Controller implements ActionListener {
private View view;
private Model model;
public Controller(View view) {
this.view = view;
model = new Model();
model.addObserver(view);
model.setValue("Fred");
}
public void actionPerformed(ActionEvent ae) {
model.setValue(view.getValue());
}
}
public class Model extends Observable {
private String value;

public Model() {
}
public Model(String value) {
this.value = value;
setChanged();
notifyObservers();
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
setChanged();
notifyObservers();
}
}
Regards
Fred
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that looks right. Essentially, you have two types of notifications when it comes to model updating the views, -- a "push" and a "pull".
A "push" is when you broadcast a message to views and your data is already included in your message:
update(updateType, data)
This way, the views don't need to query the model.
A "pull" is when the broadcast is light, -- all it contains is the update type and it is up to the views to decide which data to pull from the model:
update(updateType)
Eugene
 
Aruna Raghavan
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eugene,
Java API for Observed has only this:
public void update(Observable o, Object arg)
But how do I pass the updateType with this?
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java API for Observed has only this:
public void update(Observable o, Object arg)
But how do I pass the updateType with this?

The most flexible solution is NOT to use the predefined Observer/Observable, but your own equivalent of it. It's realy quite easy, in fact the Observer is just that:

The observable is just your model class that has the means to add, delete, and notify the observers:

Eugene.
[ January 07, 2003: Message edited by: Eugene Kononov ]
 
Aruna Raghavan
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eugene,
It was real nice of you to post the code. I expected to hear something like
"Well, implement your own" and nothing more
Thanks,
Aruna
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic