A lot of times we need a demo mode in certain projects which usually involves hardware and we want the software to operate in demo mode without the actual hardware. The functions in demo mode simulate the hardware somewhat but don't obviously operate on it.
My question is, is the proxy design pattern (or any other for that matter) a good for create demo modes in software?
Consider the example below which is simplified to be short.
class Arm
{
public:
int _x = 0;
int _y = 0;
virtual void move(int x, int y) = 0;
};
class RobotArm : public Arm
{
virtual void move(int x, int y)
{
/* assum we have actual hardware coommands here which updates variables */
//_x = x;
//_y = y;
std::cout << "hardware is not connected, can't move" << std::endl;
}
};
class RobotArmDemo : public Arm
{
virtual void move(int x, int y)
{
/* no hardware commands here, just update variables */
_x = x;
_y = y;
std::cout << "Demo Arm moved to " << _x << "," << _y << std::endl;
}
};
class Robot
{
public:
Arm *leftArm;
Arm *rightArm;
};
int main()
{
Arm * leftArm = new RobotArmDemo;
Arm * rightArm = new RobotArm;
Robot * robot = new Robot;
robot->leftArm = leftArm;
robot->rightArm = rightArm;
// perform action
robot->leftArm->move(3, 3); // this is on demo mode
robot->rightArm->move(1, 2); // this is real mode
return 0;
}
In the code above, I create one demo arm and one real arm for the robot just to show each will work. Obviously in real demo mode, all derived objects will have the demo implementation. Is this a good way to implement demo modes in software?
Aucun commentaire:
Enregistrer un commentaire