Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a GUI application. The user selects item from a comboBox (non editable). The suitable input data control is then enabled and user enters data. Then the appropriate object according to the comboBox selected item is created.

  1. I've thought of using String[] array to display the list of choices and then using conditional statements to create the object.
  2. Or create all the objects before user selects and set each object's properties accordingly after user data input.

I can't think of any better way to do this so I'd like to ask if there is one. If no, the first or the second way listed above is better? Thank you.

share|improve this question
You give too little information. Please add more details. – superM Jan 8 '13 at 10:59
User selects vehicle type from the combo box list. Then the appropriate data input control is displayed where user enters details for the selected vehicle so that a new vehicle of the selected type is created. Different vehicle types have different details so I have different data input controls for each one. Since the data is necessary for the vehicle object to be created , I thought of either instantiating all the possible vehicles, add them to the combo list and then set their data according to the user data or displaying an array of their names and wait for data input to instantiate them. – user1926116 Jan 8 '13 at 11:02
up vote 0 down vote accepted

You could use the abstract factory pattern. For example, provide a list of subclasses of an abstract class VehicleFactory (say CarFactory, BicycleFactory, ...) . Create one object of each concrete factory class and associate the object with the combo box entries. After the user has chosen the entry, use the factory to create the particular Car, Bicycle ... object.

The factories should provide all the meta-information you need to control the GUI correctly, for example, the display name of each object, or which attributes the user has to enter for the particular object. The difference to what you wrote above in your comment is that you don't associate Vehicle objects directly with the combo box, but only factories.

share|improve this answer
Thank you for the suggestion. – user1926116 Jan 8 '13 at 11:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.