请使用 设计模式 优化这段代码 if(order == tr("轴运动")){ if(orderParaWidgetMap.contains(order)){ QWidget *targetWidget=orderParaFormMap.value(order); if(targetWidget==nullptr){ targetWidget=new AxisMove_EmentParaWidget(this); orderParaFormMap.insert(order,targetWidget); } ui->stackedWidget->setCurrentWidget(targetWidget); }else{ QMessageBox::information(nullptr,"错误","未定义"); } }else if(order == tr("轴停止")){ if(orderParaWidgetMap.contains(order)){ QWidget *targetWidget=orderParaFormMap.value(order); if(targetWidget==nullptr){ targetWidget=new AxisStop_EmentParaWidget(this); orderParaFormMap.insert(order,targetWidget); } ui->stackedWidget->setCurrentWidget(targetWidget); }else{ QMessageBox::information(nullptr,"错误","未定义"); } }else if(order == tr("坐标置零")){ if(orderParaWidgetMap.contains(order)){ QWidget *targetWidget=orderParaFormMap.value(order); if(targetWidget==nullptr){ targetWidget=new AxisZero_EmentParaWidget(this); orderParaFormMap.insert(order,targetWidget); } ui->stackedWidget->setCurrentWidget(targetWidget); }else{ QMessageBox::information(nullptr,"错误","未定义"); } }
时间: 2024-03-27 07:39:10 浏览: 23
这段代码可以使用工厂模式进行优化。我们可以定义一个工厂类来负责创建不同类型的运动命令参数窗口。具体实现如下:
1. 首先,定义一个抽象的运动命令参数窗口基类 OrderParaWidget:
```cpp
#ifndef ORDERPARAWIDGET_H
#define ORDERPARAWIDGET_H
#include <QWidget>
class OrderParaWidget : public QWidget
{
Q_OBJECT
public:
explicit OrderParaWidget(QWidget *parent = nullptr);
virtual ~OrderParaWidget() = default;
virtual QString getOrderName() const = 0;
};
#endif // ORDERPARAWIDGET_H
```
2. 然后,定义三个具体的运动命令参数窗口类 AxisMove_EmentParaWidget、AxisStop_EmentParaWidget 和 AxisZero_EmentParaWidget,分别继承自 OrderParaWidget 并实现 getOrderName 函数:
```cpp
#ifndef AXISMOVE_EMENTPARAWIDGET_H
#define AXISMOVE_EMENTPARAWIDGET_H
#include "orderparawidget.h"
class AxisMove_EmentParaWidget : public OrderParaWidget
{
Q_OBJECT
public:
explicit AxisMove_EmentParaWidget(QWidget *parent = nullptr) : OrderParaWidget(parent) {}
QString getOrderName() const override { return tr("轴运动"); }
};
#endif // AXISMOVE_EMENTPARAWIDGET_H
```
```cpp
#ifndef AXISSTOP_EMENTPARAWIDGET_H
#define AXISSTOP_EMENTPARAWIDGET_H
#include "orderparawidget.h"
class AxisStop_EmentParaWidget : public OrderParaWidget
{
Q_OBJECT
public:
explicit AxisStop_EmentParaWidget(QWidget *parent = nullptr) : OrderParaWidget(parent) {}
QString getOrderName() const override { return tr("轴停止"); }
};
#endif // AXISSTOP_EMENTPARAWIDGET_H
```
```cpp
#ifndef AXISZERO_EMENTPARAWIDGET_H
#define AXISZERO_EMENTPARAWIDGET_H
#include "orderparawidget.h"
class AxisZero_EmentParaWidget : public OrderParaWidget
{
Q_OBJECT
public:
explicit AxisZero_EmentParaWidget(QWidget *parent = nullptr) : OrderParaWidget(parent) {}
QString getOrderName() const override { return tr("坐标置零"); }
};
#endif // AXISZERO_EMENTPARAWIDGET_H
```
3. 接着,定义一个工厂类 OrderParaWidgetFactory,负责根据不同的运动命令名称创建对应的参数窗口:
```cpp
#ifndef ORDERPARAWIDGETFACTORY_H
#define ORDERPARAWIDGETFACTORY_H
#include <QMap>
#include <QString>
#include "orderparawidget.h"
class OrderParaWidgetFactory
{
public:
explicit OrderParaWidgetFactory(QWidget *parent = nullptr) : m_parent(parent) {}
OrderParaWidget* createOrderParaWidget(const QString& orderName) {
if (!m_orderParaWidgetMap.contains(orderName)) {
OrderParaWidget* widget = nullptr;
if (orderName == tr("轴运动")) {
widget = new AxisMove_EmentParaWidget(m_parent);
} else if (orderName == tr("轴停止")) {
widget = new AxisStop_EmentParaWidget(m_parent);
} else if (orderName == tr("坐标置零")) {
widget = new AxisZero_EmentParaWidget(m_parent);
}
if (widget) {
m_orderParaWidgetMap.insert(orderName, widget);
} else {
QMessageBox::information(nullptr, "错误", "未定义");
}
}
return m_orderParaWidgetMap.value(orderName);
}
private:
QWidget* m_parent;
QMap<QString, OrderParaWidget*> m_orderParaWidgetMap;
};
#endif // ORDERPARAWIDGETFACTORY_H
```
4. 最后,在主程序中使用工厂类 OrderParaWidgetFactory 来创建参数窗口:
```cpp
// 定义 OrderParaWidgetFactory 对象,并传入父窗口指针
OrderParaWidgetFactory factory(this);
// 获取当前运动命令对应的参数窗口
OrderParaWidget* widget = factory.createOrderParaWidget(order);
// 切换窗口
ui->stackedWidget->setCurrentWidget(widget);
```
通过工厂模式的优化,可以将具体的对象创建过程封装在工厂类中,客户端只需要提供需要创建的对象的名称,就可以获得对应的对象,并避免了大量的 if-else 分支判断。同时,如果需要添加新的运动命令类型,只需要添加对应的参数窗口类和在工厂类中增加一条分支判断即可。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)