I wrote a parser for a certain type of binary file with recursive structure. I made its API to be similar to SAX, that is:
- the parser accepts an object of a specific interface,
- this interface has several methods called as the parsing happens:
startFoo(type, name)
,endFoo()
,datum(type, name, value)
,badEntry(errorMsg)
, etc. - there are certain promises regarding how these callbacks are called: e.g. for each
startFoo
there will be anendFoo
, with appropriate nesting, etc.
I used to think this is a variant of a Visitor pattern. However, the visitor pattern doesn't have multiple callbacks, these callbacks have different arguments, and the Visitor pattern doesn't talk about promises like in the last point. Also, strictly speaking, it's not an in-memory data structure being iterated over, but I guess this part is less important…
I also think it's not strictly an Observer pattern: there is no state to observe; no late registration for receiving events (ie. either you get all parsing events or none, you can't start in the middle); only a single object is accepted.
Is there a more proper name for this design pattern?
EDIT: As I understand it, design patterns exist to quickly and precisely communicate common code structures. However, if all I can say about the above code structure is that it is a "Strategy" pattern, or an "Observer" pattern, the communication is inefficient. Not all "Strategy" pattern implementations have interfaces with multiple methods with promises regarding the order in which they are called, etc.
I am looking for a name or a phrase that would directly communicate the set of conditions mentioned above, or at least some close approximation of them.