I've been building an application that processes documents through different filters.
I have a class (or better as an interface for creating test-doubles?) called
Document class contains many fields and therefore I was thinking about using the Builder pattern for construction.
Now, there are certain "specialized" documents that contain additional fields (let's call it
SpecialDocument). The obvious solution would be to extend the
Document class. However, how would I then (during runtime) take a
Document instance and make it a
SpecialDocument instance? Remember that the construction of the
Document instance is quite complex, therefore I think, a copy-constructor is too tedious and error-prone (when adding a filed etc.).
Another solution that I can think of is to pass the original
Document instance to the
SpecialDocument constructor and then delegate all document calls to that instance.
Or should I maybe just add those fields from
Document class and then provide optional getters, like so
Optional<String> getSpecialField? This last option seems to be the least preferable since it mixes concepts and the client would never be sure about the type of document it is dealing with (besides having to deal with the optional first).
Is there a simpler and more elegant way to model this scenario?
Thank you very much for you advice.