ハードウェアを実際に接続せずにソフトウェアを実行/シミュレートできるようにする目的でハードウェアを通常使用する特定のプロジェクトでは、デモ モードが必要になることがよくあります。デモ モードの関数は、ハードウェアをいくらかシミュレートしますが、明らかに動作しません。
私の質問は、プロキシ デザイン パターン (またはその他のパターン) は、ソフトウェアでデモ モードを作成するのに適していますか?
短く簡略化された以下の例を考えてみましょう。
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; // creating an arm in demo mode!
Arm * rightArm = new RobotArm; // this arm is the real one
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;
}
上記のコードでは、それぞれがどのように機能するかを示すために、ロボット用に 1 つのデモ アームと 1 つの実際のアームを作成しました。明らかに、実際のデモ モードでは、すべての派生オブジェクトにデモ実装が含まれます。これは、ソフトウェアにデモ モードを実装する良い方法ですか?
これは実際の大規模/中規模アプリケーションに適用できますか? 私はこのアプローチを好む傾向があります。実際のアプリケーションとデモ アプリケーションの両方でまったく同じ関数が呼び出されるため、作業が楽になり、流れが理解できるからです。あるいは、個別の「デモ」パスは、実際のアプリケーションとの類似性を失い、バラバラに成長するほぼ個別のアプリケーション/モジュールになる可能性があります。