DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

Command Design pattern C++

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:

Standard Definition:

This design pattern describe that Encapsulate the each request as object.

In layman language:

Object Oriented Callback.

Problem:

Need to handle callback for specific request and request can be triggered any time, secondly we need to process each request separately.

Solution:

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 and its callback that needs to be handled.
  • when we want to handle each request in separate class so that we can process that request separately.
  • User can trigger the request any time (during run time).

Advantage:

  • 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).

Key Points:

  • 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 sender and receiver.
  • Client set the receiver for each request.
  • At run time, invoker handle the request and pass to command.

Class Diagram:

By DigestCPP.com

Class Diagram of real example:

By DigestCpp.com

Source code:

/*
Program: Command Design Pattern 
Author: Alpha Master
Date: 21 Nov 2021
*/

//Header File
#include<iostream>


//Reciever class or callback class
class Reciver
{
        public:
        static void sendToDevice()
        {
        std::cout<<"Sending to Device"<<std::endl;
        }
        static void sendToUI()
        {
        std::cout<<"Sending to UI"<<std::endl;
        }
};


//Command class
class Command
{
	public:
	virtual void execute() = 0;
};

//concrete command
class UiCommand:public Command
{
	public:
	void execute()
	{
	std::cout<<"Sending to Callback"<<std::endl;
	Reciver::sendToUI();
	}
};

//concrete command
class DeviceCommand:public Command
{
        public:
        void execute()
        {
        std::cout<<"Sending to Callback"<<std::endl;
	Reciver::sendToDevice();
        }
};

//Invoker class
class Invoker
{
	Command* mCmd[2];
	public:
	void setCommand(int num, Command* cmd)
	{
		if(num == 0)
		mCmd[0] = cmd;
		else
		mCmd[1] = cmd;
	}
	void invoke(Command* cmd) {cmd->execute();}
	void userInput(int choice)
	{
		if(choice == 1)  //UI command
			invoke(mCmd[0]);
		else if(choice == 2)
			invoke(mCmd[1]);
		else
			std::cout<<"Wrong Choice"<<std::endl;
	
	}
};

int main()
{
	std::cout<<"Command Design Pattern"<<std::endl;
	//client create concreate command and set it
	Invoker inv;
	inv.setCommand(0, new UiCommand);
	inv.setCommand(1, new DeviceCommand);
	
	int choice = 0;
	while(1)
	{
		std::cout<<"Enter the choice and for exit 3"<<std::endl;
		std::cin>>choice;
		inv.userInput(choice);
		if(choice == 3)
		break;		
        }
	
	return 0;
}

Output:

Command Design Pattern
Enter the choice and for exit 3
1
Sending to Callback
Sending to UI
Enter the choice and for exit 3
2
Sending to Callback
Sending to Device
Enter the choice and for exit 3
0
Wrong Choice
Enter the choice and for exit 3
3
Wrong Choice

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer