Question

My friend and I were discussing about the design of a simple command processor - accepts string commands, splits it and looks for keywords (add, subtract etc), executes those commands and returns a result. So far, so good.

But now what if one wants the same commands to work differently when the command processor is in different modes (complex mode, real mode etc).

Which design pattern could be used here, keeping in mind that it should be easy to add more keywords and modes in the future?

Was it helpful?

Solution

Nice thing about design patterns is that their names describe problems they solve. So, you should check Command pattern.

Command pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

Command pattern consists command, receiver, invoker and client.

Modes object will be particularly interesting for you, since you want to add modes of execution.

Invoker object keeps information about command executions, and has a responsibility for executing different modes for commands.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top