I am designing an application on paper at the moment and I'm in a bit of a pickle design wise.
The goal of the application is that I have car factories that can give me blueprints of their car models. A blueprint is not a car but it describes the eventual car that can be built form it, e.g., model name, options on the vehicle like airconditioning etc.
For this I have created an interface ICarBlueprint. The methods on this interface could include getModelName() or getOptionList() for example. These methods have to work on each instance of car blueprint, be it an Opel or a Volkswagen car blueprint.
A factory, e.g., Opel or Volkswagen, can give a list of their car blueprints. For factories I have thus created an interface ICarFactory. This interface contains the method getAllBlueprints() which returns a List of ICarBluePrint objects. For the Opel factory this would then be OpelBlueprint instances, which implement the interface ICarBlueprint.
In my main program I want to get all the possible car blue prints that a user can pick from. An example could be:
for(ICarFactory f : factories)
allblueprints.addAll(f.getAllBlueprints());
I then want to iterate over that list and if one of those blueprints is the car I want, I want to build it. However, a car blue print can only be built by the factory that has generated this blueprint. So a blueprint of an Opel car can only be built by an instance of OpelFactory, which would thus implement the interface ICarFactory.
Building a car would result in an actual car object. For this I have created the interface ICar. Again, for each factory I would have classes that implement this interface.
The design issue Im having starts here.
To build a blueprint I can add a method named build() to the ICarFactory interface. The main program can then simply pick a car instance, e.g., goodcarBlueprint and ask a car factory to build it.
So in order to make sure that a blueprint is built by the correct factory I was thinking about adding a method getFactory() to the interface ICarBlueprint and build() to the ICarFactory interface. This would then allow me to build a blueprint in its respective factory like so:
ICarBlueprint goodcarBlueprint = // Assigned somewhere else
ICar x = goodcarBlueprint.getFactory().build(goodcarBluePrint);
However, my gut feeling is telling me that this really bad design. Any help about the proper way to design this is appreciated.
Aucun commentaire:
Enregistrer un commentaire