I've been implementing an enhanced Shunting-Yard algorithm for parsing an arithmetic expression. One aspect of the algorithm, is that it maintains a Queue
, and a Stack
.
In my implementation, the Queue
contains Expressions
and Operators
.
The Stack
contains Operators
and Parenthesis
.
Expressions
, Parenthesis
, and Operators
have nothing in common that warrants any two of them having a shared interface.
Approaches:
My current implementation consists of
Expression
andOperator
implementing aINotParanthesis
.Operator
andParanthesis
implement aINotExpression
. I then declareQueue <INotParanthesis>
, andStack <INotExpression>
.I don't like this implementation - these interfaces seem very much a hack for the purpose of cleaner algorithm code. I also believe that interfaces should describe what an object is, as opposed to what it isn't.
On the other hand, I also don't want to use collections of
<Object>
, as it can be difficult to be certain of the correctness of such code.The only one I've come up with, so far, is implementing my own
NonParanthesisQueue
andNonExpressionStack
containers. This has the advantage of more consistent type checking on objects getting pulled out of those containers - and the disadvantage of a lot more code.
Are there any reasonable alternatives to my approaches?