I am developing a swing app, where I have a Factory class which provide Component keeping Singleton in mind. Like:
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;
}
}
I am using this component as:
add(ComponentFactory.getLibraryTableScrollPane())
Also I make a ListenerFactory class which provides various Listeners of Swing/AWT.
Is this pattern has any flaws? Can I use a same component or listener with two concurrently visible parent component?
Thanks in advance.
It has a major flaw: it promotes a lack of encapsulation by making every component globally accessible. This can lead very quickly to spaghetti code where every object uses any other object, instead of having a short list of dependencies providing encapsulated methods.
Another problem is with the implementation: the synchronization is unnecessary, since Swing components are not thread-safe, and may only be used from the event dispatch thread. You should thus only have the EDT calling your methods, which makes synchronization unnecessary.
Finally, a component may only have one parent component. If the same compoentnmust be displayed in two different frames, for example, you'll need two instances of this component.