I completely agree with Orion Edwards, and as a rule, I approach the problem; but lately I'm starting to see some patterns (!) of insanity.
For more complex tasks, I usually use something like an interpreter (or strategy ), which uses a builder (or factory ) to create each piece of data.
For streaming, the entire parser will look like an adapter , adapting from a stream object to an object stream (which is usually just a queue).
For your example, there will probably be one builder for a complete data structure (from head to EOF) that internally uses collectors for internal data elements (powered by the interpreter). As soon as the EOF meets, the object will be emitted.
However, the objects created in the switch statement in some factory function are probably the easiest way for many smaller tasks. Also, I like to keep my data objects unchanged, as you never know when someone suffocates concurrency down their throats :)
Henrik gustafsson
source share