!2 refactor: #I7XMJG 工厂改为单例模式

Merge pull request !2 from songdragon/refactor-factory
This commit is contained in:
格物方能致知 2023-08-31 15:10:30 +00:00 committed by Gitee
commit f9a9cb99f8
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
4 changed files with 28 additions and 21 deletions

View File

@ -40,9 +40,8 @@ public class OpenFile implements EventHandler<ActionEvent> {
*/ */
@Override @Override
public void handle(ActionEvent event) { public void handle(ActionEvent event) {
FileChooserFactory factory = new TextFileChooserFactory();
// 显示文件选择对话框并获取选中的文件 // 显示文件选择对话框并获取选中的文件
File file = factory.createFileChooser( File file = TextFileChooserFactory.getInstance().createFileChooser(
UiResourceBundle.getContent(TextConstants.OPEN), UiResourceBundle.getContent(TextConstants.OPEN),
null, null,
null, null,

View File

@ -88,12 +88,10 @@ public class RenameFile implements EventHandler<ActionEvent> {
* @since 2023/8/31 21:47 * @since 2023/8/31 21:47
*/ */
private void handleRenameRelevanceFile(JNotepadTab jnotepadtab) { private void handleRenameRelevanceFile(JNotepadTab jnotepadtab) {
// 创建文件选择器对话框工厂选择文本文件对话框工厂
FileChooserFactory factory = new TextFileChooserFactory();
// 获取原始文件对象 // 获取原始文件对象
File file = (File) jnotepadtab.getUserData(); File file = (File) jnotepadtab.getUserData();
// 获取应用窗口并绑定 // 获取应用窗口并绑定
File newFile = factory File newFile = TextFileChooserFactory.getInstance()
.createFileChooser( .createFileChooser(
UiResourceBundle.getContent(TextConstants.RENAME), UiResourceBundle.getContent(TextConstants.RENAME),
jnotepadtab.getText(), jnotepadtab.getText(),

View File

@ -67,10 +67,11 @@ public class SaveFile implements EventHandler<ActionEvent> {
* @see LogUtil * @see LogUtil
*/ */
protected void saveTab(Class<?> currentClass) { protected void saveTab(Class<?> currentClass) {
FileChooserFactory factory = new TextFileChooserFactory();
JNotepadTab selectedTab = UiUtil.getJnotepadtab(); JNotepadTab selectedTab = UiUtil.getJnotepadtab();
if (selectedTab != null) { if (selectedTab == null) {
File file = factory.createFileChooser( return;
}
File file = TextFileChooserFactory.getInstance().createFileChooser(
UiResourceBundle.getContent(TextConstants.SAVE_AS), UiResourceBundle.getContent(TextConstants.SAVE_AS),
selectedTab.getText(), selectedTab.getText(),
null, null,
@ -85,5 +86,4 @@ public class SaveFile implements EventHandler<ActionEvent> {
selectedTab.setText(file.getName()); selectedTab.setText(file.getName());
} }
} }
}
} }

View File

@ -13,6 +13,16 @@ import java.io.File;
*/ */
public class TextFileChooserFactory implements FileChooserFactory { public class TextFileChooserFactory implements FileChooserFactory {
private TextFileChooserFactory() {
}
private static final TextFileChooserFactory INSTANCE = new TextFileChooserFactory();
public static TextFileChooserFactory getInstance() {
return INSTANCE;
}
/** /**
* 创建原始文件选择对话框 * 创建原始文件选择对话框
* *