gpt4 book ai didi

c++ - Qt:信号和槽 vs C++:消息传递

转载 作者:行者123 更新时间:2023-11-28 03:07:16 24 4
gpt4 key购买 nike

我有两个类(class)

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 对象中

目前我有这两种方式,

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_;
}

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

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

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

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

谢谢。

最佳答案

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

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

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

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

没有“一种明确的方法”可以做到这一点。只有更好和更坏的。

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

对于“纯 C++”方法,您可以引入一个抽象接口(interface)来监听驱动程序名称更改(或者更一般地用于监听通用名称更改)。我在这里指的基本上是实现 Observer图案。简化的实现可以例如看起来像这样:

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”类来封装这些关系,而不是直接在主窗口中进行。

关于c++ - Qt:信号和槽 vs C++:消息传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19381085/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com