• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Problems with needing to construct object in different ways with same constructor args

Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a class that will have a couple of 'parsers'. These parsers are responsible for parsing a string into a certain class

Ok so assume a vin number has letters in it so it has to be a string not an integer/number.

I want to be able to create my garage with a cars vin, or my cars make/model. Once I have created the garage, I want to be able get my cars make/model or vin. So I want to be able to construct the garage with a vin or a make model, but obviously I cannot have two constructors with the same String argument.

Basically I want be able to go from
car -> makeModel
makeModel -> car

car -> vin
vin -> car

Which is why I have those parsers, but how do I get a car object inside my garage and still be able to go back and forth?
Posts: 28271
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand that. The "parsers" are simply tools for creating objects, aren't they? If you want to keep track of the created objects and their relationships, you would need some other tool for that.

If I were designing a system involving cars and VINs then a car would have a VIN. This would give me a mapping from car to VIN. If I wanted the opposite mapping then I would build a Map<String, Car> to provide that.

Such a Map could of course be updated by the factory methods which create Car objects or by the Car method (or constructor) which assigns a VIN to a Car.
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, that will fit in a stocking
    Bookmark Topic Watch Topic
  • New Topic