The Song in English Script:
The Song in English Translation:
It takes time to gain expertise…
Today, with just a Google search, we can get a hell of a lot of information about a detailed analysis of any specific software framework. But when I started in the mid ‘90s with MFC/Visual C++, it was an entirely different ball game. So, i struggled, i banged my head, i travelled a long distance in the hot summer, i did everything a normal human being can do to decipher the internal code structure of MFC/VC++. I remember, i started studying the Gang of Four Design Pattern book around 2001 - 2002. And then the engineer in me was again born. The analytical mind to dig into a framework code to understand different aspects of Object oriented Design Principle was again honed.
And hence, when I googled today about the use of Chain Of Responsibility Design pattern in MFC’s Command Routing architecture, I got a hell of a lot of big documents describing all the applicable Gang of Four Design Pattern in just a flash.
And then I remember, in Bangalore/Trivandrum, my everyday night outs in front of an ordinary PC to understand by taking notes and creating an image of the exact flow in the mind to get an uncluttered mind about this very Design pattern in the MFC command routing architecture.
Universe, today let me tell you about the road an inquisitive mind has to travel to decipher a specific maze out of his own. I will tell you my confusion about mapping the design used in the MFC command routing when i started to look at it from a designer’s perspective.
The first thing that, as the name suggests, it has to do something with the command pattern. And, as the basic principle of command pattern is to execute a specific chunk of code, whenever the command is asked to be executed, and obviously this is what is happening here. Hence, i started looking at the whole flow with just keeping Command Pattern in mind. However, somewhat was missing. I spent two three days about that missing part. it was not exactly command pattern. Then what it could have been.
Then suddenly, one early morning,in Trivundrum, i jumped out of bed and looked at the rough sketch of the flow and then it clicked probably, the commands are passed using the Chain Of Responsibilty design pattern, because its absolutely fixed in nature, has to happen only on the path of the chain - no exception.
And voila!!!
After that, it was just a cake walk.
Everything just fell in line.
The difficult code started behaving like bhiggi billi.
No camouflage.
Clear cut.
The intention of the designer became absolutely clear.
And, the scientist in me became happy just to figure it out.
Let’s not forget that a scientist never thinks of just making money to remain happy and contended and motivated.
Otherwise, the ancient sages of #bharat would not have become sages just for acquiring knowledge and wisdom.
For certain people, the calling is higher.
We had Swami Vivekananda, Netaji Subhash Chandra Bose, Rishi Aurobindo and many more who were purposefully erased from the minds of the young generation of #bharat to push a certain political agenda of making us impotent and feeble so that we forget WhoWeAre as a nation.
I remember, after few months, before i left Trivandrum, there was a telephonic interview from Chennai where i told about this basic architecture of MFC command routing and the interviewer was just flattened.
So, my suggestion to all of the learners of Object oriented principles, don’t study Command Design Pattern and Chain of Responsibility as two separate entities.
Rather, think of them as the right jugalbandi where the commands from the Command Pattern will be passed along the chain of the Chain of Responsibility Design pattern, and MFC command routing architecture is a practical framework where this basic principle has been applied.
And, hence, when my currently 11-year-old son #ridit completed his study about the command pattern, I refactored his code to fit into the Chain of Responsibility design pattern, which will be his next area of study.
See, the dots are so nicely connected backwards.
I think I submitted my first article on the use of a specific Design pattern in MFC (i can’t remember which one I did) at CodeGuru with a UML diagram and everything possible to explain it to a layman, and my joy was unbounded when, after a few days, it was published. And then the confidence was built, and the journey to contribute to the learning community started.
So you know, it's hard work, determination, sweat and blood, night outs in front of the computer, and sheer resilience that today I can work as an independent #guru for my currently 11-year-old son #ridit.
So, a few days back, when Ridit completed the Command Design Pattern, and he finished the learning process by offering an online training explaining the same design pattern while programming in C++ in camera, I decided to train him on Chain Of Responsibility as the right application of the Command Pattern, and this idea I took from my initial days of study of the MFC framework.
So, you see, the dots ultimately get connected backwards…
Here's my efforts in which a kind of application where a command will be routed along with the path of a predetermined chain of the Chain Of Responsibility design pattern has been depicted.
Enjoy.
My application, written in C++, showcases the use of the Chain of Responsibility to route a Command.
Here's a pictorial view...
Client (main)
|
|
Creates Task
|
v
task.execute()
|
v
Receiver Level 1
|
Can handle it?
/ \
Yes No
| |
v v
Done Receiver Level 2
|
Can handle it?
/ \
Yes No
| |
v v
Done Receiver Level 3
|
Can handle it?
/ \
Yes No
| |
v v
Done Unhandled
I refactored my son's code of the Command Pattern, and here is the result…
/*
* Level.h
*
* Created on: May 28, 2022
* Author: som
*/
#ifndef LEVEL_H_
#define LEVEL_H_
enum class Level { Level1, Level2, Level3 };
enum class CommandType { Tea, Coffee };
#endif // LEVEL_H_
#ifndef COMMAND_H_
#define COMMAND_H_
#include <string>
#include "Level.h"
class Receiver; // Forward declaration
class Task {
private:
CommandType type;
std::string description;
Level level;
Receiver* receiver;
public:
Task(CommandType inType, std::string inDescription, Level inLevel, Receiver* inReceiver);
virtual ~Task() = default;
void execute();
CommandType getCommandType() const;
Level getLevel() const;
std::string getDescription() const;
Receiver* getReceiver() const;
};
#endif
// COMMAND_H_
/*
* Task.cpp
*
* Created on: May 28, 2022
* Author: som
*/
#include "Command.h"
#include "Receiver.h"
Task::Task(CommandType inType, std::string inDescription, Level inLevel, Receiver* inReceiver)
: type(inType), description(std::move(inDescription)), level(inLevel), receiver(inReceiver) {}
CommandType Task::getCommandType() const { return type; }
Level Task::getLevel() const { return level; }
std::string Task::getDescription() const { return description; }
Receiver* Task::getReceiver() const { return receiver; }
void Task::execute() {
if (receiver) {
receiver->handleCommand(this);
}
}
/*
* Reciever.h
*
* Created on: 26-May-2022
* Author: ridit
*/
#ifndef RECEIVER_H_
#define RECEIVER_H_
#include <memory>
class Task;
class Receiver {
protected:
std::unique_ptr<Receiver> successor; // Exclusive ownership of the next chain link
public:
Receiver() = default;
explicit Receiver(std::unique_ptr<Receiver> inSuccessor);
virtual ~Receiver() = default;
virtual bool handleCommandImpl(Task* command) = 0;
void handleCommand(Task* command);
Receiver* getSuccessor() const;
};
#endif // RECEIVER_H_
/*
* Receiver.cpp
*
* Created on: May 28, 2022
* Author: som
*/
#include "Receiver.h"
#include "Command.h"
Receiver::Receiver(std::unique_ptr<Receiver> inSuccessor)
: successor(std::move(inSuccessor)) {}
void Receiver::handleCommand(Task* command) {
bool handledAtThisNode = handleCommandImpl(command);
if (!handledAtThisNode && successor) {
successor->handleCommand(command);
}
}
Receiver* Receiver::getSuccessor() const {
return successor.get();
}
/*
* ReceiverLevel1.h
*
* Created on: May 28, 2022
* Author: som
*/
#ifndef RECEIVER_LEVEL1_H_
#define RECEIVER_LEVEL1_H_
#include "Receiver.h"
class ReceiverLevel1 : public Receiver {
public:
explicit ReceiverLevel1(std::unique_ptr<Receiver> inSuccessor = nullptr);
~ReceiverLevel1() override = default;
bool handleCommandImpl(Task* command) override;
};
#endif // RECEIVER_LEVEL1_H_
/*
* ReceiverLevel1.cpp
*
* Created on: May 28, 2022
* Author: som
*/
#include "ReceiverLevel1.h"
#include "Command.h"
#include <iostream>
ReceiverLevel1::ReceiverLevel1(std::unique_ptr<Receiver> inSuccessor)
: Receiver(std::move(inSuccessor)) {}
bool ReceiverLevel1::handleCommandImpl(Task* command) {
if (command->getCommandType() == CommandType::Tea) {
if (command->getLevel() == Level::Level1) {
std::cout << "TeaCommand is handled at Level 1\n";
return true;
}
}
return false; // Propagate down the chain
}
/*
* ReceiverLevel2.h
*
* Created on: May 28, 2022
* Author: som
*/
#ifndef RECEIVER_LEVEL2_H_
#define RECEIVER_LEVEL2_H_
#include "Receiver.h"
class ReceiverLevel2 : public Receiver {
public:
explicit ReceiverLevel2(std::unique_ptr<Receiver> inSuccessor = nullptr);
~ReceiverLevel2() override = default;
bool handleCommandImpl(Task* command) override;
};
#endif // RECEIVER_LEVEL2_H_
/*
* ReceiverLevel2.cpp
*
* Created on: May 28, 2022
* Author: som
*/
#include "ReceiverLevel2.h"
#include "Command.h"
#include <iostream>
ReceiverLevel2::ReceiverLevel2(std::unique_ptr<Receiver> inSuccessor)
: Receiver(std::move(inSuccessor)) {}
bool ReceiverLevel2::handleCommandImpl(Task* command) {
if (command->getLevel() == Level::Level2) {
std::cout << "Task handled at Level 2 (Type: "
<< (command->getCommandType() == CommandType::Tea ? "Tea" : "Coffee")
<< ")\n";
return true;
}
return false; // Propagate down to Level 3
}
/*
* ReceiverLevel3.h
*
* Created on: May 28, 2022
* Author: som
*/
#ifndef RECEIVER_LEVEL3_H_
#define RECEIVER_LEVEL3_H_
#include "Receiver.h"
class ReceiverLevel3 : public Receiver {
public:
explicit ReceiverLevel3(std::unique_ptr<Receiver> inSuccessor = nullptr);
~ReceiverLevel3() override = default;
bool handleCommandImpl(Task* command) override;
};
#endif // RECEIVER_LEVEL3_H_
/* RECEIVERLEVEL3_H_ */
/*
* ReceiverLevel3.cpp
*
* Created on: May 28, 2022
* Author: som
*/
#include "ReceiverLevel3.h"
#include "Command.h"
#include <iostream>
ReceiverLevel3::ReceiverLevel3(std::unique_ptr<Receiver> inSuccessor)
: Receiver(std::move(inSuccessor)) {}
bool ReceiverLevel3::handleCommandImpl(Task* command) {
if (command->getLevel() == Level::Level3) {
std::cout << "Task handled at Level 3 (Final fallback level)\n";
return true;
}
// If it reaches the end of the chain and is still unhandled:
std::cout << "Error: Command could not be handled by any receiver in the chain.\n";
return false;
}
/*
* Main.cpp
*
* Created on: May 28, 2022
* Author: som
*/
#include <iostream>
#include <memory>
#include "Level.h"
#include "Command.h"
#include "ReceiverLevel1.h"
#include "ReceiverLevel2.h"
#include "ReceiverLevel3.h"
int main() {
// Construct the chain from back to front:
// Level1 points to Level2, Level2 points to Level3, Level3 points to nullptr.
auto chain = std::make_unique<ReceiverLevel1>(
std::make_unique<ReceiverLevel2>(
std::make_unique<ReceiverLevel3>()
)
);
// Create various tasks with different types and levels
Task task1(CommandType::Tea, "Morning Tea", Level::Level1, chain.get());
Task task2(CommandType::Coffee, "Afternoon Espresso", Level::Level2, chain.get());
Task task3(CommandType::Tea, "Special Brew", Level::Level3, chain.get());
std::cout << "--- Executing Task 1 (Level 1 Tea) ---\n";
task1.execute();
std::cout << "\n--- Executing Task 2 (Level 2 Coffee) ---\n";
task2.execute();
std::cout << "\n--- Executing Task 3 (Level 3 Tea) ---\n";
task3.execute();
return 0;
}
Here are the UML class diagram and sequence diagram of this work.
Enjoy...


No comments:
Post a Comment