I inherited the following code:
(Some names are changed to protect the innocent.)
package foo.bar.baz;
import j开发者_开发百科avax.swing.JPanel;
//Main panel in the GUI
class DemoRootPanel extends JPanel {
private final MainGUIClass demo;
DemoRootPanel(MainGUIClass demo) {
super();
this.demo = demo;
}
public MainGUIClass getDragAndDropPanelsDemo() {
return demo;
}
}
I didn't immediately see why it was important, so I grepped through the source tree to see it being used. These are all the results:
$ ack -i rootpanel
DemoRootPanel.java
5:class DemoRootPanel extends JPanel {
8: DemoRootPanel(MainGUIClass demo) {
MainGUIClass.java
70: private final DemoRootPanel rootPanel;
124: rootPanel = new DemoRootPanel(MainGUIClass.this);
821: containerPanels.add(new ContainerPanel(rootPanel));
846: containerPanels.add(new ContainerPanel(rootPanel));
1777: ContainerPanel newContainer = new ContainerPanel(rootPanel);
ContainerPanel.java
30: DemoRootPanel rootPanel;
49: public ContainerPanel(DemoRootPanel rootPanel) {
51: this.rootPanel = rootPanel;
and
$ ack getDragAndDropPanelsDemo
DemoRootPanel.java
13: public PileSortGUI getDragAndDropPanelsDemo() {
So as far as I see, it's completely unnecessary. But this is my first job, I haven't been to college yet, etcetera, so I'm wondering if it's not some idiom that's important but unfamiliar to me.
PS - I'm also relatively new to SO. I believe this question can be answered definitively, but it does seem less technical than normal. If it's not okay, let me know and I'll take it down. (Or just mod it :P )
It looks like this is just an empty JPanel which additionally contains a reference to an object of the main class.
It could be that this could be simply replaced by a plain JPanel
(ommiting the demo
argument).
It could be that someone planned to used this class for some goal but forgot this later. Or someone misunderstood and thought that every GUI object one uses must have an own subclass. The comment //Main panel in the GUI
looks like this.
If you have someone who wrote this code, ask him (or her) - otherwise I would just replace its use with a simple JPanel to cut down the number of classes. (You have a version control system, don't you?)
精彩评论