我正在开发一个swing应用程序,其中有一个工厂类,它考虑到单例模式提供组件。例如:
public final class ComponentFactory {
private static LibraryFrame libraryFrame;
private static LibraryTableScrollPane libraryTableScrollPane;
public static synchronized LibraryFrame getLibraryFrame() {
if (libraryFrame == null) {
libraryFrame = new LibraryFrame();
}
return libraryFrame;
}
public static synchronized LibraryTableScrollPane getLibraryTableScrollPane() {
if(libraryTableScrollPane == null) {
libraryTableScrollPane = new LibraryTableScrollPane(getLibraryTable());
}
return libraryTableScrollPane;
}
}
我将会把这个组件用作:
add(ComponentFactory.getLibraryTableScrollPane())
我还创建了一个ListenerFactory类,提供Swing/AWT的各种监听器。
这个模式有什么缺陷吗?我能否在两个同时可见的父组件中使用同一组件或监听器?
先感谢您。