I am following a Java tutorial related to the implementation of the observer pattern (using Swing) and I have some doubts. My doubts are not related to the observer pattern but about the architecture of this tutorial application (that is based on something like an MVC logic)
So it contains an Application class that contains the main() method that is the application entry point:
public class Application {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
runApp();
}
});
}
public static void runApp() {
Model model = new Model();
View view = new View(model);
Controller controller = new Controller(view, model);
view.setLoginListener(controller);
}
}
As you can see here it is declared a Model object (for the data), a Controller object that actually simply perform the operation related to the click on a button defined into my view, this is the View code:
public class Controller implements LoginListener {
private View view;
private Model model;
public Controller(View view, Model model) {
this.view = view;
this.model = model;
}
@Override
public void loginPerformed(LoginFormEvent event) {
System.out.println("Login event received: " + event.getName() + "; " + event.getPassword());
}
}
and the View object that implement a login form (username and password) with the JButton okButton (its click is handled from the previous Controller object):
public class View extends JFrame implements ActionListener {
private Model model;
private JButton okButton;
private JTextField nameField;
private JPasswordField passField;
private JPasswordField repeatPassField;
private LoginListener loginListener;
public View(Model model) {
super("MVC Demo");
this.model = model;
nameField = new JTextField(10);
passField = new JPasswordField(10);
repeatPassField = new JPasswordField(10);
okButton = new JButton("Create user");
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.anchor = GridBagConstraints.LAST_LINE_END;
gc.gridx = 1;
gc.gridy = 1;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(100, 0, 0, 10);
gc.fill = GridBagConstraints.NONE;
add(new JLabel("Name: "), gc);
gc.anchor = GridBagConstraints.LAST_LINE_START;
gc.gridx = 2;
gc.gridy = 1;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(100, 0, 0, 0);
gc.fill = GridBagConstraints.NONE;
add(nameField, gc);
gc.anchor = GridBagConstraints.LINE_END;
gc.gridx = 1;
gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(0, 0, 0, 10);
gc.fill = GridBagConstraints.NONE;
add(new JLabel("Password: "), gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.gridx = 2;
gc.gridy = 2;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(0, 0, 0, 0);
gc.fill = GridBagConstraints.NONE;
add(passField, gc);
gc.anchor = GridBagConstraints.LINE_END;
gc.gridx = 1;
gc.gridy = 3;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(0, 0, 0, 10);
gc.fill = GridBagConstraints.NONE;
add(new JLabel("Repeat password: "), gc);
gc.anchor = GridBagConstraints.LINE_START;
gc.gridx = 2;
gc.gridy = 3;
gc.weightx = 1;
gc.weighty = 1;
gc.insets = new Insets(0, 0, 0, 0);
gc.fill = GridBagConstraints.NONE;
add(repeatPassField, gc);
gc.anchor = GridBagConstraints.FIRST_LINE_START;
gc.gridx = 2;
gc.gridy = 4;
gc.weightx = 1;
gc.weighty = 100;
gc.fill = GridBagConstraints.NONE;
add(okButton, gc);
okButton.addActionListener(this);
// Database db = new Database();
// Database db = Database.getInstance();
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
try {
Database.getInstance().connect();
} catch (Exception e1) {
JOptionPane.showMessageDialog(View.this, "Unable to connect to database.",
"Error", JOptionPane.WARNING_MESSAGE);
e1.printStackTrace();
}
}
@Override
public void windowClosing(WindowEvent e) {
Database.getInstance().disconnect();
}
});
setSize(600, 500);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
String password = new String(passField.getPassword());
String repeat = new String(repeatPassField.getPassword());
if (password.equals(repeat)) {
String name = nameField.getText();
fireLoginEvent(new LoginFormEvent(name, password));
} else {
JOptionPane.showMessageDialog(this, "Passwords do not match.",
"Error", JOptionPane.WARNING_MESSAGE);
}
}
public void setLoginListener(LoginListener loginListener) {
this.loginListener = loginListener;
}
public void fireLoginEvent(LoginFormEvent event) {
if (loginListener != null) {
loginListener.loginPerformed(event);
}
}
}
It seems that it follow the classic MVC logic but if you look into the View class you find this code:
addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
try {
Database.getInstance().connect();
} catch (Exception e1) {
JOptionPane.showMessageDialog(View.this, "Unable to connect to database.",
"Error", JOptionPane.WARNING_MESSAGE);
e1.printStackTrace();
}
}
@Override
public void windowClosing(WindowEvent e) {
Database.getInstance().disconnect();
}
});
I am not so into Swing and I don't exactly know what is the WindowAdapter object (what is it?) but it seems to me that in this code is add a listener that handle the event related to the main windows opening.
So when the main window of my application is opened the View obtain a new connection to the database, when it is closed the connection is colsed.
My doubt is: but is this an antipattern? Opening the connection to the database is not a responsability of the Controller?
Tnx
Aucun commentaire:
Enregistrer un commentaire