我有时会在我的Java应用程序中显示对话框。
目前,Controller类是(期望在我的模型上只调用getter的一些例外),就像我的模型和UI之间的调解器一样。
但是我的UI知道我的控制器,我的控制器知道我的UI。
每当我添加一个新对话框时,我都会在控制器和视图类中添加一个方法。
使用设计模式是否有更优雅的方式使用新用户对话框扩展我的程序?
为了说明我现在的互动情况,我会添加一些代码段。
来自我的用户界面的代码
itmEmailSettings.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
controller.showEmailSettingsDialog();
}
} );
更多用户界面代码
public void showEmailSettingsDialog(String host, int port, int authMode,
String user, String pass, String fromEMail, String fromName) {
EmailSettingsDialog d = new EmailSettingsDialog(
host, port, authMode,
user, pass, fromEMail, fromName
);
d.createJDialog( mainFrame.getFrame() ).setVisible(true);
if(d.isValid()){
controller.changeEmailSettings( d.getHost(), d.getPort(), d.getAuthMode(), d.getFromEMail(), d.getFromName(), d.getUser(), d.getPass() );
}
}
控制器代码:
public void showEmailSettingsDialog() {
try{
if(!pm.hasProjectFileAccess()){
mainFrame.showNoProjectfileAccess();
return;
}
ProgrammSettingsRepository pr = Utils.getProgrammSettingsRepository(pm);
String host = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_HOST);
int port = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_PORT)==null?0:Integer.parseInt( pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_PORT) );
int authMode = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_SSL_MODE)==null?0:Integer.parseInt( pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_SSL_MODE) );
String user = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_USER);
String pass = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_PASSWORD);
String fromEMail = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_FROM_EMAIL);
String fromName = pr.retreive(ProgrammSettingsRepository.KEY_EMAIL_FROM_NAME);
menuView.showEmailSettingsDialog(host, port, authMode, user, pass, fromEMail, fromName);
}catch(SQLException e){
throw new RuntimeException(e.getMessage(), e);
}
}
public void changeEmailSettings(String host, int port, int authMode,
String fromEMail, String fromName, String user, String pass) {
try {
ProgrammSettingsRepository pr = Utils.getProgrammSettingsRepository(pm);
pr.store( ProgrammSettingsRepository.KEY_EMAIL_HOST , String.valueOf( host ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_PORT , String.valueOf( port ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_SSL_MODE , String.valueOf( authMode ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_USER , String.valueOf( user ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_PASSWORD, String.valueOf( pass ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_FROM_EMAIL , String.valueOf( fromEMail ));
pr.store( ProgrammSettingsRepository.KEY_EMAIL_FROM_NAME , String.valueOf( fromName ));
pr.store(ProgrammSettingsRepository.KEY_EMAIL_SETTINGS_CONFIGURED, "true");
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
答案 0 :(得分:3)
据我所知,从UI调用Controller,然后从Controller调用UI来显示对话框。控制器执行一些计算,然后调用UI以显示对话框。
如果您对UI类实现IReportable。
public interface IReportable {
public void showYesNoDialog(//all needed params);
public void showSimpleDialog(//all needed params);
.
.
.
}
public class DialogController() {
private IRportable _reportable;//delegator
public DialogController(IRportable _reportable) {//bridge pattern.
_reportable = reportable;
}
public void showEmailDialog() {
//calculations
_reportable.showSimpleDialog(//params);
}
public void showCustomerDialog() {
//calculations
_reportable.showYesNoDialog(//params);
}
}
public class UIClass implements IReportable {
private DialogController _dialogController;
public UIClass() {
_dialogController = new DialogController();
}
public void someMethod() {
if() {
}
...
_dialogController.showEmailDialog();
}
public void someOtherMethod() {
if() {
}
...
_dialogController.showCustomerDialog();
}
@Override
public void showYesNoDialog(//all needed params) {
//code here to show dialog according to params.
}
@Override
public void showSimpleDialog(//all needed params) {
//code here to show dialog according to params.
}
}
这样你就可以依赖接口而不是单个ui组件。您可以将接口更改为抽象类,以使所有UI类具有相同的功能......