Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用策略设计模式实现输入类_C++_Design Patterns_Strategy Pattern - Fatal编程技术网

C++ 使用策略设计模式实现输入类

C++ 使用策略设计模式实现输入类,c++,design-patterns,strategy-pattern,C++,Design Patterns,Strategy Pattern,我对面向对象编程有点陌生。我必须实现一个界面,应考虑到不同的输入类型,即操纵杆,鼠标,xbox等 一个运动控制器类将使用该接口,一个播放器类将实现该运动类 我很难把这些想法写成代码。主要是接口类中应该包含的内容 到目前为止,我是这样想象的: Class InputInterface { //What goes here? } class Movement : InputInterface { } //Other classes that inherit InputInterface

我对面向对象编程有点陌生。我必须实现一个界面,应考虑到不同的输入类型,即操纵杆,鼠标,xbox等

一个运动控制器类将使用该接口,一个播放器类将实现该运动类

我很难把这些想法写成代码。主要是接口类中应该包含的内容

到目前为止,我是这样想象的:

Class InputInterface
{
  //What goes here? 

}

class Movement : InputInterface
{
}

//Other classes that inherit InputInterface


class PlayerShip
{
  public: 

   MovementController* movementController;
}

所以我知道大多数“控制器”类(操纵杆、360控制器等)使用返回布尔值或浮点值的函数。因此,我可能在接口类中有数据成员,它们的值可以由接口类中的成员函数设置

策略模式的最佳做法是保持简单。与客户机对象相关的方法将进入接口,并仅公开客户机调用该方法所需知道的有关内部对象的信息。您将在客户机对象之外构造具体对象

template<typename T>
class IInput
{
public:
    virtual ~IInput() { } // if you want to clean up via this interface instead
                          // of the concrete object you will need a virtual destructor
    virtual T Poll() = 0;
};
模板
第二类输入
{
公众:
虚拟~IInput(){}//如果您想改为通过此接口进行清理
//对于具体对象,您将需要一个虚拟析构函数
虚拟T Poll()=0;
};
我已将类型设置为通用类型,但您不必这样做

您的播放器对象可能如下所示:

#include <memory>

class Player
{
    std::shared_ptr<IInput<float>> velocityx;
    float displacementx;
public:
    Player(std::shared_ptr<IInput<float>> v)
        :velocityx(v)
    {
       displacementx = 0;
    }
    void Update(float elapsedSeconds)
    {
        displacementx += elapsedSeconds * velocityx->Poll();
    }
};
std::shared_ptr<Player> player = std::make_shared<Player>(std::make_shared<MouseMover>());
#包括
职业选手
{
std::共享速度;
浮动位移x;
公众:
播放机(std::shared_ptr v)
:速度x(v)
{
位移x=0;
}
无效更新(浮动延迟秒)
{
置换X+=elapsedSeconds*velocityx->Poll();
}
};
这样构造(想象MouseMover是你的具体类)

Player播放器(std::make_shared());
或者像这样:

#include <memory>

class Player
{
    std::shared_ptr<IInput<float>> velocityx;
    float displacementx;
public:
    Player(std::shared_ptr<IInput<float>> v)
        :velocityx(v)
    {
       displacementx = 0;
    }
    void Update(float elapsedSeconds)
    {
        displacementx += elapsedSeconds * velocityx->Poll();
    }
};
std::shared_ptr<Player> player = std::make_shared<Player>(std::make_shared<MouseMover>());
std::shared_ptr player=std::make_shared(std::make_shared());