Design Pattern means that “we had some problem then we found the solution and gave name to this solution” and command design is also one of the solution so we need to find the problem that command design pattern has solved and how ?
This design pattern comes under Behavioral Category (It tells that how objects are communicating with each other (objects have been created already)).
C++ Command Design pattern:
This design pattern describe that Encapsulate the each request as object.
In layman language:
Object Oriented Callback.
Need to handle callback for specific request and request can be triggered any time.
Encapsulate the each request in separate class and register its callback and each derived class will represent specific request, this solution called as “command design pattern”
When we will use command design pattern:
- When we have many request that needs to be handled.
- when we want to handle each request in separate class.
- Loose coupling (each request is represented in separate class so we can handle the different request in different class).
- Flexible (We can add new request at any time without polluting existing code).
- Need to create separate class(concrete command) for each request.
- Invoker does not have any knowledge about operation or receiver.
- This design pattern decouple the object and receiver.
- Client set the receiver for each request.
- At run time, invoker handle the request and pass to command.