Problem in choosing a design pattern for my java project
I am developing a Java Desktop Application. From a Swing GUI, a user enters 5 inputs through 5 checkboxes as follows:
CheckBox1: Scrape Name
CheckBox2: Scrape Address
CheckBox3: Scrape Phone Number
CheckBox4: Scrape State
CheckBox5: Scrape Country
I have made 5 functions, each for each checkbox.
public static String getName(String pageContent) {
...
}
public static String getAddress(String pageContent) {
...
}
:
:
Now, I have written a code that call开发者_C百科s these functions according to the input of the user. E.g.
public static void main(String [] args) {
List<String> list = new ArrayList<String>();
...
code to populate the list
...
String name = null;
String address = null;
String phone = null;
String state = null;
String country = null;
for(String pageContent : list) {
if(CheckBox1.isSelected()) {
name = getName(pageContent);
}
if(CheckBox2.isSelected()) {
address = getAddress(pageContent);
}
...
similar code for the remaining 3 methods
...
code to store the data in variables (name, address...) in the database
...
}
}
Now, In actual the number of checkBoxes are around 20 and the user has the option to save its configuration so that he don't need to select his choices again and again for subsequent scraping. He just load the configuration (stored in .xml file) and start scraping.
So, my question is as follows:
Q1. The list
can contains 1000s
of strings so the 20
conditions (20 if blocks in the for loop) are checked each time, so there will be 1000 * 20 comparisions
to scrape 1000 strings. Is the above approach (shown in code) correct?
One thing that looks wrong is your multiple if
statements inside your for
loop, and unique variables to hold the scraped data. This means if your methods retrieve values for multiple pageContent
s, the variables will be written over and the previous value lost.
Is that what you want? If not, you should refactor your for
loop and your if
statements, maybe by just passing the entire list to the getName
, getAddress
, etc. methods, or some other way, but it's not entirely clear how your code is supposed to work. Depending on what the data looks like, the code could probably be optimized, by maybe sorting the list...
Do you think there is anything wrong or unwieldy about the current design?
I don't think it is clear yet to what design pattern this may be factored, if at all. One thing isn't clear for me, what exactly is the desired output of the scraping process?
That said, here are my 2 cents: The 1000 * 20 comparisons most likely don't mean anything performance wise. You could restructure the code so that the checks are made only once with polymorphic objects or something, but that will most likely be way overcomplicated. Something similar to the strategy pattern perhaps. I wouldn't do it yet though.
I would however make a very simple 'scraper' class, which contains exactly the same code as in your for-loop but the choices are set with booleans or something. Then have a seperate method create a scraper object from either the gui or xml file. From there you can refactor further if the need arises.
For a very simple application as yours seems, choosing a classic design pattern for the overall application framework might be more of an academic exercise than anything else. Such a design pattern might turn out to over-complicate the program. In reality, the approach you have taken works well for small aplications. One improvement for readability would be to methodize the process within your for loop.
That said, from your example it seems that you are testing the state of your checkboxes from within the main
method. I would expect this code to reside, instead, within a method like actionPerformed
(inherited from the ActionListener
interface), or some similar Observer that would react to Swing events. I would suggest a GUI design that provides a button to trigger the action, and your app would register as an ActionListener
. This would allow for the user to select the checkboxes that apply to his query, and then trigger the action. Within the actionPerformed
method you could move your trigger to begin the parsing of your page content. You will want to be wary of the threading constraints that Swing provides, and move your database calls to a worker thread. If you are not already familiar, take a look at SwingWorker.
Hardly, anyone can tell you which design pattern is suitable for your problem. I would suggest you to, just study those design pattern which you think are suitable for you problem. you find more than one design thet can fix you problem. Just implement them, write code and then again study your reuqirement. Again go with other design pattern and implement that. Eventually you will end with correct design. This is the way you can learn the design pattern and difference between them, that will help you in future to choose the correct design. Design pattern are ready solution suggested by GOF, they do not solve any problem directly. They just give you right direction.
精彩评论