c++ - 维护当前选定的对象是否很好地使用状态模式?

标签 c++ oop design-patterns state-pattern

状态模式的典型场景涉及的状态大多不同,例如closed_connection_stateopen_connection_state。在我的例子中,所有状态基本相同,但操作需要应用于当前选定的对象。

通常这样的事情是使用指向当前选定对象的索引变量完成的,但是使用状态模式是更好的实现方式,就像下面的示例一样?

class Object
{
    std::string _name;
public:
    Object(std::string name) : _name(name)
    {

    }

    void Perform()
    {
        std::cout << _name << " Perform called\r\n";
    }
};

class CurrentObject
{
    Object* _a;
    Object* _b;
    Object* _current;

public:

    CurrentObject(Object* a, Object* b) : _a(a), _b(b)
    {
        _current = a;
    }

    void Perform()
    {
        _current->Perform();

        // Update _current logic goes here, lets just switch
        // the state whenever `Perform` is called.
        if (_current == _a)
            _current = _b;
        else
            _current = _a;
    };

};

int main()
{
    Object a("a"); // program can be in a state when `a` is the current object.
    Object b("b"); // or b can become the current object as result of an operation on current object

    CurrentObject current(&a, &b); // it assigns the defaults

    // assume Perform() does its thing but it also needs to change the current selected object.
    // In this example, we assumes the current selection object is always swapped.
    current.Perform(); // operates on `a`, the default
    current.Perform(); // operates on `b` due state changed in above line
    current.Perform(); // operates on `a` doe to state changed in above line again
}

最佳答案

这绝对是一件合理的事情,如果你的状态成倍增加(状​​态机往往如此),这可能会变得有点难以维护,但它实际上是一个非常好的 OO 风格的状态机实现。

您可能希望您的状态(a 和 b)扩展一个通用的抽象状态,这样当所有状态的功能都相同时,您不必在每个对象中都实现它。

为了扩展,您可能还想“命名”您的状态并将它们放入哈希表中,一旦它扩展(记住,在编程中您有 1 个或多个)添加新状态不会对您的状态进行代码更改机器——但我假设您已经有了类似的东西,只是针对问题缩小了范围。

另请注意,要切换您不想直接执行的状态(例如您的示例),您可能需要一个方法(setState)在执行方法返回时更改状态,而不是在执行方法本身或 while它正在运行。事实上,您可以让 perform 返回一个字符串,指示它是下一个所需状态..

根据评论编辑:

我所说的命名您的状态的意思是:

class CurrentObject
{
    Object* _a;
    Object* _b;
    Object* _current;
    ...

您可能有类似的东西(请原谅我的 java 语法,C# 不是我的主要语言,但我知道它在功能上非常相似)

class CurrentObject
{
    Hashtable states=new Hashtable();
    Object* _current;

    public addState(String stateName, Object* state)
    {
        states.put(stateName, state)
    }

    public void Perform()
    {
        String nextState = _current->Perform();
        if(nextState != null)
            setState(nextState);
    }
    public void setState(String stateName)
    {
        _current = states.get(stateName);
    }

}

您的调用代码会执行如下操作:

currentObject = new CurrentObject()
currentObject.addState("state a", _a);
currentObject.addState("state b", _b); 
currentObject.setState("state a");
currentObject.perform();
...

我忽略了很多初始化和错误检查。

您的状态机目前只有一个事件:“Perform()”。您可能会发现您需要其他事件,这会使事情变得有点复杂(在 Java 中,我可能会使用反射或注释来解决该问题,但不确定 C# 将如何做到这一点)。

关于c++ - 维护当前选定的对象是否很好地使用状态模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52319626/

相关文章:

c++ - 使用派生类构造函数初始化对象

c++ - 读取结构时出现未处理的异常

oop - 我可以说构造函数是方法吗?

php - 我应该将对象或普通数据传递到 View 中吗?

android - 如何识别 Android 中的 TAP(摇动)模式?

wpf - MVVM View 模型与 View 之间的关系

c++ - 在 WinDbg 中开始调试的基本设置+任务是什么?

c++ - wxWidgets 中 UpdateWindow() 的等价物是什么?

.net - 是否需要封装Collections?

language-agnostic - 是否有任何模式或是否有任何用于继承数据/对象的标准术语?