shlogg · Early preview
Prashant Mishra @prashantrmishra

Command Pattern In Java: Wrapping Requests As Commands

Command pattern: a request wrapped in an object as command, passed to invoker, executed by corresponding object. Follows OCP & SIP solid principles. Example: stock market with BuyOrder & SellOrder classes.

Command pattern is one of the behavioral patterns, where a request is wrapped inside an object as command and that object is passed to an invoker, the invoker then looks for an appropriate object that can handle this command and passes the command to the corresponding object, the object then executes the command.
This also follows the OCP solid principle
Let's take an example of the stock market, where Stock is a request that should be bought or sold(command), this Stock is wrapped under Order, this Order is then sent to the broker (Invoker), the broker then analyses the Order to decide whethe...