Results 1 to 1 of 1
- 12-08-2010, 01:34 AM #1
Member
- Join Date
- Sep 2010
- Location
- Oregon, usa
- Posts
- 69
- Rep Power
- 0
Interface usage and the double-dispatch pattern
I have an interface:
And two classes that implement MyComponent:Java Code:public interface MyComponent { public void addToPanel(JPanel panel); public String[] getUserInput(); }
OK, now for the parts that I get confused on. (PLEASE correct me if I'm wrong or you see a better way!! Thankyou!)Java Code:public class TextBox implements MyComponent{ private JTextField[] textbox; public TextBox() { textbox = new JTextField[10]; for (int i=0;i<textbox.length;i++) { textbox[i] = new JTextField(); } } public void addToPanel(JPanel panel) { for (int i=0;i<textbox.length;i++) { panel.add(textbox[i]); } } public String[] getUserInput() { //..... code to return user input from JTextFields.... // ... return string[]; } } public class CheckBox implements MyComponent{ private JCheckBox[] checkbox; public CheckBox(List list) { checkbox = new JCheckBox[list.size()]; for(int i=0;i<checkbox.length;i++) { checkbox[i] = new JCheckBox((String)list.get(i)); } } public void addToPanel(JPanel panel) { for (int i=0;i<checkbox.length;i++) { panel.add(checkbox[i]); } } public String[] getUserInput() { //..... code to return user checks from JCheckBox[].... // ... return string[]; } }
I have a class the has a MyComponents variable. I've had to set up an initialize method that instantiates the MyComponent variable based on a passed parameter, but after that the MyComponent variable calls the methods and does not care whether it is textbox or checkbox... and if I want to add another class that creates something different, I think I can. As long as it implements the MyComponent interface AND I add it to the initializeMyComonent method, it should work within this class.
My questions: Is this a correct way to implement an interface? Is it similar to the double-dispatch pattern (see post link below)?Java Code:public class SearchPanel extends JPanel { private MyComponent component; public SearchPanel(int type) { //code to initialize components such as JPanel panel; initializeMyComponent(type); component.addToPanel(panel); } initializeMyComponent(int type) { if (type == 0) { //for ease of posting (have other class for this) component = new TextBox(); } else if (type == 1) { component = new CheckBox(list); // just assume you have the list } } public String[] getInputs() { String[] result = component.getUserInputs(); return result; } }
Many thanks in advance for any/all responses/critiques/links/etc!!
Chris
Assign Different Objects to Array - Jos had responded to this original post with an example of a Visitor interface, and I still had questions about it. :):cool: It's all here: http://download.oracle.com/javase/6/docs/api/
Similar Threads
-
Class pattern to generate following pattern:-
By vxs in forum New To JavaReplies: 5Last Post: 07-14-2010, 11:15 PM -
Request dispatch from one web app to another
By karthikus in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06-15-2010, 07:31 PM -
Dispatch simulated events
By kusanagi97 in forum AWT / SwingReplies: 1Last Post: 10-12-2009, 02:20 PM -
Dynamic Method Dispatch
By sandeshforu in forum New To JavaReplies: 0Last Post: 09-15-2009, 05:18 PM -
Error: Exception during event dispatch!
By Javid in forum AWT / SwingReplies: 1Last Post: 08-02-2008, 01:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks