1

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.

2
  • You give too little information. Please add more details.
    – superM
    Jan 8, 2013 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. Jan 8, 2013 at 11:02

1 Answer 1

0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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