Qt:Signals and Slots vs C++:Message Passing [英] Qt:Signals and Slots vs C++:Message Passing

查看:55
本文介绍了Qt:Signals and Slots vs C++:Message Passing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班

class Car
{
// class Car related methods
public:
setDriversName(QString);
private:
String driversName_;    // This information comes from class DriversName

//Must get the drivers name info, when a particular event in this object occurs;
}

class DriversName
{
//class DriversName related methods
String getDriversName();
private:
String driversName_;
}

所以,现在我想在两个类之间进行通信,更具体地说,我希望 Car 类从 DriversName 类中获取驾驶员的姓名 事件发生在 Car Object.

So, now i want to communicate between the two classes, more specifically i want class Car to get the driver's name from the DriversName class when a particular event occours in Car Object.

目前我有这两种方式,

C++ Message passing:

class Car
{
// class Car related methods
public:
setDriversNameObject(DriversName& );
setDriversName()
{
   driversName_ =  DriversName.getDriversName();
}

private:
DriversName driversName_;    // So after setting the Driverclass object, i can directly 
//access the driver's name, whenever the event occurs
String driversName_;
}

class DriversName
{
//class DriversName related methods
String getDriversName();

private:
DriversName driversName_;
}

Qt

In mainWindow class:

connect(carObject,eventHasOccured(),this,updateDriversName());


void MainWindow::updateDriversName()
{
    carObject->setDriversName(driversNameObject.getDriversName);
}

Class Car
{
    Q_OBJECT
    signals:
    emit eventHasOccured();

    public:
    setDriversNameObject(DriversName& );

    private:
    DriversName driversName_;              
}

Class DriversName
{
    Q_OBJECT

    String getDriversName();

    private:
    DriversName driversName_;    
}

这两种解决方案肯定会奏效.以下是我的问题:

Both the Solutions will definitely work. Here are my questions:

1) 就OO而言,上述方法是否有任何缺陷原则.

1) Is there any flaw in the above methods, with respect to OO principles.

2) 哪种情况是最标准的方式处理 ?

2) Which is the most standard way this situation can be dealt with ?

3) 有没有其他更好的方法来处理给定的情况.

3) is there any other better method to handle the given situation.

谢谢.

推荐答案

1) 就面向对象原则而言,上述方法是否有任何缺陷.

1) Is there any flaw in the above methods, with respect to OO principles.

第一种方法将 Car 方法与 DriversName 类紧密结合.

The first approach couples the Car method closely to the DriversName class.

第二个将您的应用程序与 Qt 耦合.如果您无论如何都在整个应用程序中使用它,那不是什么大问题,但应该考虑.此外,它将业务逻辑代码移动到 mainwindow 类中,这可能不是一个好主意.

The second couples your application to Qt. If you use it throughout your whole application anyway, that's not much of a problem, but it should be considered. Also, it moves business logic code into the mainwindow class, which might not be such a good idea.

2) 处理这种情况的最标准方法是什么?

2) Which is the most standard way this situation can be dealt with ?

没有一种确定的方法"可以做到这一点.只是更好和更糟.

There is no "one definitive way" of doing this. Just better and worse ones.

3) 有没有其他更好的方法来处理给定的情况.

3) is there any other better method to handle the given situation.

对于纯 C++"方法,您可以引入一个抽象接口来侦听驱动程序名称更改(或者甚至可能更一般地用于侦听通用名称更改).我在这里所指的基本上是实现 观察者 模式.一个简化的实现可以例如看起来像这样:

For the "pure C++" approach, you could introduce an abstract interface for listening to driver name changes (or maybe even more generically for listening to generic name changes). What I'm referring to here is basically implementing the Observer pattern. A simplified implementation could e.g. look like this:

class NameChangeListener
{
public:
    virtual void onNameChange(std::string const & newName) =0;
};

// This assumes change is initiated in DriversName class - not sure if this is correct
// Your code doesn't show the actual triggering of the change
class DriversName
{
public:
// ... other stuff
    setNameChangeListener(NameChangeListener* ncl)
    {
        changeListener = ncl;
    }
    void setName(std::string const & newName)
    {
        // ... other stuff
        if (changeListener)
            changeListener->onNameChange(newName);
    }
private:
    // ..
    NameChangeListener* changeListener;
};

class Car: public NameChangeListener
{
public:
// ... other stuff
    void onNameChange(std::string const & newName) {
        driversName = newName;
    }
};

// somewhere outside:
Car c1;
DriversName n;
n.setNameChangeListener(c1);

对于 Qt 方法,最好引入一个额外的Controller"类来封装这些关系,而不是直接在主窗口中进行.

For the Qt approach, it might be good to introduce an additional "Controller" class encapsulating such relations, instead of doing it directly in the main window.

这篇关于Qt:Signals and Slots vs C++:Message Passing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆