I have a series of static utility methods of this form:
StringBuffer Util.doA(String arg0, String arg1, SomeEnum e);
StringBuffer Util.doB(String arg0, String arg1);
StringBuffer Util.doC(String arg0, String arg1, String arg2,String arg3);
StringBuffer Util.doD(String arg0, String arg1, String arg2,String arg3, AnotherEnum e);
etc
I want to display a JDialog that is created dynamically e.g. via builder or other pattern and once a button is pressed I want the appropriate utility method to be called depending on the type of dialog.
E.g. If JDialog is oftype A
the actionlistener of button should call the Util.doA
passing as arguments data from the input fields and if JDialog is of type B
I want the actionlistener of button to call Util.doB
etc.
Types A, B etc are my custom types. Are not mandatory to be used. I just have them to distinguish between each JDialog.
JDialog of type A differs from JDialog of type B in some of the input fields. But the overall appearance is similar.
Totally there are about 20 types and 20 corresponding util methods.
Is there a standard patter开发者_如何学编程n to this problem I have?
I started extending JDialog so that each custom JDialog creates itself properly and the method of actionlistener calls the appropriate utility method, but I end up with 20 new classes.
So I thought if there is a better approach to this.
Any ideas?
Thank you
Put the corresponding util method inside the 20 types. If there is any duplication between the types you might be able to move it to a super class.
As to your question, I think this is the optimal approach in your case. If you have the dialog and util code all in one class and nothing more, then it is clear what that class's purpose is. It also means that if you need to remove, change, add a new type you are only having to delete one class, modify one class, or create one new class. Keeping all the related code together prevent's "Shotgun surgery".
精彩评论