Sunday, 22 November 2015

Design Pattern - Command Pattern in C++

Design Pattern - Command Pattern in Cpp with code examples:

Purpose of the command pattern:

This pattern encapsulates a request as an object, thereby letting us parameterize other objects with different requests, queue or log requests, and support undoable operations.

For example, SwitchOn object has the right method (execute(), and actually, doing Switch->On()) of the receiver (Switch object). This is possible, the command object has the pointer to the Switch receiver as its member. So, when asked to perform an action on a receiver, we just feeding the command object to the invoker.

The invoker which is named as control in the code make a request of a Command object by calling
that object's execute() method. Then, since the method knows what the receiver is, it invokes the action on that receiver.

Here is an example code:

Will update the code later

Output from the run:
The Switch is on
The Switch is off

No comments: