I have a UI-dialog something like this: You must choose a book from a list. Optionally, you can either choose a publisher (another class) from a list or enter the publisher-name as as a string.
I think this gives me 3 types as the output from the dialog.
- book
- book with publisher-class
- book with publisher-string
How would you model this in objects? It seems to me that the having a book base-class, and then two subclasses for publisher and publisher name is the correct choice. Are there any alternatives, perhaps favoring composition that would give a better model?
I’ll try to explain a bit more. A book doesn’t need to have a publisher. The publisher object is not the same as a publisher-name entered as a string.
You must
-choose a book from an existing list
You can one of the following
-choose a publisher from an existing list or
-you can enter a publisher name or
-you can fill nothing about the publisher
Still calls for a custom result object. Now you have three fields a Book Object, a Publisher Object, and a Publisher String. You then pass it to the code that can deal with it intelligently. You could add methods to address custom processing needs. But in the end it is a specialized result of THAT dialog and should not be some subfield of book or publisher or any other object.
If the book is nothing you know you got an error because you need a book. You have four combinations of Publisher Object and Publisher_String to deal with as well. Of this indicates to me that you need a class to deal specifically with the result.